NReco LambdaParser

repository·master·Indexed 43 days ago

https://github.com/nreco/lambdaparser

A 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.

Tokens
947
Snippets
3
Records
4
Agent score
5%

What's inside LambdaParser

  1. What is NReco LambdaParser?

    master
    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.
  2. Optimize performance with expression caching

    master

    The UseCache property controls whether parsed expressions are cached.

    • Default behavior: UseCache is true. This improves performance for repeated evaluations of the same expression string.
    • Best Practice: Use a singleton instance of LambdaParser to 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;
  3. Evaluate string expressions with LambdaParser

    master

    To evaluate an expression, instantiate NReco.Linq.LambdaParser and call the Eval method. You can provide expression arguments as a Dictionary<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 using new dictionary{ {"a", 1} }, new {"a":1}, or {"a":1, "b":2}.
    • Local Variables: You can use var a = 5; syntax if LambdaParser.AllowVars is set to true.
    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
  4. Configure custom value comparison behavior

    master

    By default, LambdaParser uses ValueComparer. You can inject a custom ValueComparer instance into the LambdaParser constructor to control how values are compared, specifically regarding null handling and error suppression.

    ValueComparer Options

    • NullComparison: Determines how null is handled.
      • ValueComparer.NullComparisonMode.MinValue: null is treated as the minimal possible value for any type (similar to .NET IComparer).
      • ValueComparer.NullComparisonMode.Sql: null is not comparable with any type, including another null (similar to SQL behavior).
    • SuppressErrors: If set to true, exceptions during type conversion during comparison are suppressed. Instead of throwing, the comparison simply results in false.
    var valComparer = new ValueComparer() { NullComparison = ValueComparer.NullComparisonMode.Sql };
    var lambdaParser = new LambdaParser(valComparer);