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

Expression Evaluator for System.Math

Mar 10, 2015 at 11:10 AM
Edited Mar 10, 2015 at 11:35 AM

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
Mar 11, 2015 at 12: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();
            var ce1 = new CompiledExpression("Convert.ToInt16(Math.Sin(4) + Math.Cos(5))") { TypeRegistry = types };