nreco / lambdaparser

Runtime parser for string expressions (formulas, method calls). Builds dynamic LINQ expression tree and compiles it to lambda delegate.
http://www.nrecosite.com/
MIT License
307 stars 55 forks source link

it is not support Convert #49

Open DNetORM opened 5 months ago

DNetORM commented 5 months ago

var res = lambdaParser.Eval("Convert.ToInt32(\"111\")", varContext)

this is not work well. how to support more system namespace class and functions?

VitaliyMF commented 5 months ago

For security reasons it is not possible to call any static methods like "Convert.ToInt32"; only evaluation context that is available is defined explicitly (via variables). If you need to provide some 'API' that should be available in expressions you may use this approach:

class MathFunctions {
  public double Pow(double x, double y) => Math.Pow(x,y);
}

varContext["Math"] = new MathFunctions();
Console.WriteLine(lambdaParser.Eval("Math.Pow(2,2)", varContext));

(this can be Convert variable that holds an object that has ToInt32 method, for instance).