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
309 stars 55 forks source link

How to throw exception? #48

Open DNetORM opened 7 months ago

DNetORM commented 7 months ago
       var valComparer = new ValueComparer() { NullComparison = ValueComparer.NullComparisonMode.Sql };
       var lambdaParser = new NReco.Linq.LambdaParser(valComparer);
       var varContext = new Dictionary<string, object>();
       varContext["pi"] = 3.14M;
       var res=  lambdaParser.Eval("pi>what", varContext); 

I don't have a variable name called what,So I hope I can catch an exception and give me some hints for checking,Instead of directly returning a false to me,How to catch an exception? or tell me variable name error?
VitaliyMF commented 7 months ago

If you need to implement some kind of 'validation' (without actual evaluation of the expression), it is possible to get a list of expression's parameters in this way:

string exprStr = "pi>what";
var lambdaParser = new LambdaParser();
var expr = lambdaParser.Parse(exprStr);
var exprParams = lambdaParser.GetExpressionParameters(expr);

To validate parameters during evaluation you may use LambdaParser.Eval(string expr, Func<string,object> getVarValue) overload and implement your own behaviour inside getVarValue handler.