I need to evaluate a mathmatical expression that is presented to me as a string in C#. Example noddy but gets the point across that the string as the expression.
I need the evaluate to then populate an int.
There is no Eval() in C# like in others langugaes…
String myString = '3*4';
Edit:
I am on VS2008
Tried the Microsoft.JScript. = Its deprecated method (but still complies – warning)
However the Microsoft.JScript dll that I have doens work on
public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args);
Complains that there is a missing ‘;’ go figure…
EDIT 2
Solution – was the codeDom one – it worked for as there are no security issue – only me ever going to be running the code. Many thanks for the replies …
And the link to the new Dragon Book awesome
EDIT 3
Matt dataTable.Compute() also works – even better for the security conscious. (parameter checking noted)
The way I see it, you have two options – use an expression evaluator or construct, compile and run C# code on the fly.
I would go with an expression evaluator library, as you do not have to worry about any security issues. That is, you might not be able to use code generation in medium trust environments, such as most shared hosting servers.
Here is an example for generating code to evaluate expressions: http://www.vbforums.com/showthread.php?t=397264