This project has moved. For the latest updates, please go here.

Expression Evaluator for System.Math

Mar 10, 2015 at 12:10 PM
Edited Mar 10, 2015 at 12:35 PM
Hi,

I have copied the code from Parser.cs, it is failed to parse Register Default Types such as Convert and Math. I need to evaluate an expression like "Convert.ToInt16(Math.Sin(4) + Math.Cos(5))". Can any body tell me how to achieve this function using .Eval(). While doing this i am getting this error Parser error at position 4: Unknown type or identifier 'Math'

Thanks in advance
Rajanikanth
Developer
Mar 11, 2015 at 1:58 AM
You have to call RegisterDefaultTypes explicitly.

Create a TypeRegistry, call RegisterDefaultTypes() or register any symbols tor types you need, then assign it to your compiled expression before calling Compile(), ScopeCompile() or Eval()
            var types = new TypeRegistry();
            types.RegisterDefaultTypes();
            var ce1 = new CompiledExpression("Convert.ToInt16(Math.Sin(4) + Math.Cos(5))") { TypeRegistry = types };
            Console.WriteLine(ce1.Eval());