I want to take a math expression that takes variables and print (assign to string) the formula with the variables filled in.
int iTwo = 2;
int iResult = 0;
iResult = iTwo * iTwo;
string theString = (iTwo * iTwo).ToString();
In in the above code iResult = 4 and theString = “4”
I would like to do something that fills in the variables and returns the math expression like:
theString = (iTwo * iTwo).ExpressionToString();
and end up with theString = “2 * 2”;
Thoughts?
You could use the
Expression<>type.You’ll need to make the method more complex to parse things more complex than a simple binary expression, but that’s the general idea. You could just do
e.Body.ToString(), but due to the way anonymous types are made for your lambdas, that can get ugly results, e.g.:"(value(TestApp.Program+<>c__DisplayClass3).iTwo * value(TestApp.Program+<>c__Dis.playClass3).iTwo)"