NReco LambdaParser
repository·master·Indexed 43 days ago
https://github.com/nreco/lambdaparserA high-performance runtime expression evaluator for .NET that converts string-based formulas and logic into compiled LINQ expression trees. It supports arithmetic, comparisons, conditionals, and object accessors, and is compatible with .NET Framework 4.5, .NET Standard 1.3, and .NET Standard 2.0.
What's inside LambdaParser
- NReco LambdaParser is a runtime parser for string expressions, such as formulas, method calls, and property/field/array accessors. It builds dynamic LINQ expression trees and compiles them into lambda delegates. Types are resolved at runtime, providing behavior similar to dynamic languages. It is compatible with .NET Framework 4.5, .NET Standard 1.3, and .NET Standard 2.0.
Optimize performance with expression caching
masterThe
UseCacheproperty controls whether parsed expressions are cached.- Default behavior:
UseCacheistrue. This improves performance for repeated evaluations of the same expression string. - Best Practice: Use a singleton instance of
LambdaParserto take advantage of the cache. - Memory Management: If you are evaluating a very large number of unique expressions and want to save memory, set
UseCache = false.
var lambdaParser = new LambdaParser(); lambdaParser.UseCache = false;- Default behavior:
Evaluate string expressions with LambdaParser
masterTo evaluate an expression, instantiate
NReco.Linq.LambdaParserand call theEvalmethod. You can provide expression arguments as aDictionary<string, object>or via a callback delegate.Supported features include:
- Arithmetic:
+,-,*,/,% - Comparisons:
==,!=,>,<,>=,<= - Conditionals: Ternary operator
boolVal ? whenTrue : whenFalse - Accessors: Object properties, method calls, indexers, and delegate invocation.
- Collections: Create arrays using
new []{ 1, 2, 3 },new [1, 2, 3], or[1, 2, 3]. Create dictionaries usingnew dictionary{ {"a", 1} },new {"a":1}, or{"a":1, "b":2}. - Local Variables: You can use
var a = 5;syntax ifLambdaParser.AllowVarsis set totrue.
var lambdaParser = new NReco.Linq.LambdaParser(); var varContext = new Dictionary<string,object>(); varContext["pi"] = 3.14M; varContext["one"] = 1M; varContext["two"] = 2M; varContext["test"] = "test"; // Evaluate a conditional arithmetic expression Console.WriteLine( lambdaParser.Eval("pi>one && 0<one ? (1+8)/3+1*two : 0", varContext) ); // --> 5 // Evaluate a method call Console.WriteLine( lambdaParser.Eval("test.ToUpper()", varContext) ); // --> TEST- Arithmetic:
Configure custom value comparison behavior
masterBy default,
LambdaParserusesValueComparer. You can inject a customValueComparerinstance into theLambdaParserconstructor to control how values are compared, specifically regardingnullhandling and error suppression.ValueComparer Options
- NullComparison: Determines how
nullis handled.ValueComparer.NullComparisonMode.MinValue:nullis treated as the minimal possible value for any type (similar to .NETIComparer).ValueComparer.NullComparisonMode.Sql:nullis not comparable with any type, including anothernull(similar to SQL behavior).
- SuppressErrors: If set to
true, exceptions during type conversion during comparison are suppressed. Instead of throwing, the comparison simply results infalse.
var valComparer = new ValueComparer() { NullComparison = ValueComparer.NullComparisonMode.Sql }; var lambdaParser = new LambdaParser(valComparer);- NullComparison: Determines how