18
There are many algorithms to evaluate expressions, for example:
Is there any way to evaluate any mathematical expression using C# .net reflection or other modern .net technology?
There are many algorithms to evaluate expressions, for example:
Is there any way to evaluate any mathematical expression using C# .net reflection or other modern .net technology?
Although using compiler services is a simple and efficient solution, it raises serious security issues if the expression is entered by a user, because it could execute virtually anything.
There's another very simple solution that is much more secure : take advantage of the JScript Eval function. You just need to follow these steps :
Create a js file named JsMath.js :
class JsMath
{
static function Eval(expression : String) : double
{
return eval(expression);
};
}
Compile it into a class library :
jsc /t:library JsMath.js
Reference the JsMath library in your C# project, and use it like that :
double result = JsMath.Eval(expression);