Eval Expression.NET Documentation

repository·master·Indexed 19 days ago

https://github.com/zzzprojects/eval-expression.net

A high-performance library for evaluating, compiling, and executing dynamic C# code and expressions at runtime. It provides a faster alternative to standard C# reflection via the Z.Expressions.Eval package, supporting Eval.Execute for immediate execution and Eval.Compile for transforming expressions into reusable delegates. The library supports a wide range of C# syntax, including lambda expressions, generic types, and C# 6.0 features like string interpolation and null conditional member access.

Tokens
1.6K
Snippets
5
Records
7
Agent score
17%

What's inside Eval Expression.NET

  1. Supported C# syntax and features

    master

    Eval Expression.NET supports a wide range of C# syntax, including:

    • All C# Keywords and Operators
    • Constant Folding
    • Extension Methods
    • Generic Types
    • Lambda Expressions
    • Number Suffixes

    It also supports C# 6.0 features even when running on older runtimes like C# 4.0, such as:

    • String interpolation
    • typename
    • Null conditional member access
  2. Evaluate expressions with Eval.Execute

    master

    Use Eval.Execute<T> to evaluate and immediately return the result of a C# expression at runtime. You can provide parameters using several different methods:

    1. Anonymous Types: Pass an object where property names match the expression identifiers.
    2. Argument Position: Use indexed placeholders like {0}, {1} to map directly to the arguments provided.
    3. ExpandoObject: Use dynamic objects to provide named parameters.
    4. Dictionary: Pass a Dictionary<string, object> where keys match the expression identifiers.
    // Parameter: Anonymous Type
    int result = Eval.Execute<int>("X + Y", new { X = 1, Y = 2} );
    
    // Parameter: Argument Position
    int result = Eval.Execute<int>("{0} + {1}", 1, 2);
    
    // Parameter: Class Member (ExpandoObject)
    dynamic expandoObject = new ExpandoObject();
    expandoObject.X = 1;
    expandoObject.Y = 2;
    int result = Eval.Execute<int>("X + Y", expandoObject);
    
    // Parameter: Dictionary Key
    var values = new Dictionary<string, object>() { {"X", 1}, {"Y", 2} };
    int result = Eval.Execute<int>("X + Y", values);
  3. Compile expressions with Eval.Compile

    master

    Use Eval.Compile<T> to transform a C# expression into a reusable delegate. This is more efficient than Eval.Execute if you need to run the same expression multiple times with different values.

    Supported delegate types include:

    • Func<...> for expressions that return a value.
    • Action<...> for expressions that perform an operation without returning a value.

    You can define parameters using indexed placeholders ({0}, {1}) or by explicitly providing parameter names as strings.

    // Delegate Func
    var compiled = Eval.Compile<Func<int, int, int>>("{0} + {1}");
    int result = compiled(1, 2);
    
    // Delegate Action
    var compiled = Eval.Compile<Action<int, int>>("{0} + {1}");
    compiled(1, 2);
    
    // Named Parameter
    var compiled = Eval.Compile<Func<int, int, int>>("X + Y", "X", "Y");
    int result = compiled(1, 2);
  4. Evaluate and execute code with Eval.Execute

    master

    Use Eval.Execute<T> to evaluate and immediately run C# code or expressions at runtime. This method supports several input patterns:

    • Anonymous Classes: Pass an anonymous object to provide context for variables in the expression.
    • Argument Position: Use {0}, {1}, etc., to map arguments to positions in the expression.
    • Class Members: Use objects like ExpandoObject to provide context.
    • Extension Methods: Call .Execute<T>(context) directly on a string containing the expression.
    // @nuget: Z.Expressions.Eval
    using Z.Expressions;
    
    // Using Anonymous Class
    int result = Eval.Execute<int>("X + Y", new { X = 1, Y = 2 });
    
    // Using Argument Position
    int result2 = Eval.Execute<int>("{0} + {1}", 1, 2);
    
    // Using ExpandoObject
    // dynamic expandoObject = new ExpandoObject();
    // expandoObject.X = 1; 
    // expandoObject.Y = 2;
    // int result3 = Eval.Execute<int>("X + Y", expandoObject);
    
    // Using Extension Methods
    string s = "X + Y";
    int result4 = s.Execute<int>(new { X = 1, Y = 2 });
  5. Compile expressions into delegates with Eval.Compile

    master

    If you need to execute the same expression multiple times, use Eval.Compile<TDelegate> to compile the code into a high-performance delegate. This avoids the overhead of repeated parsing and evaluation.

    • Custom Delegate: Pass the expression and the names of the parameters to Eval.Compile<TDelegate>.
    • Extension Methods: Call .Compile<TDelegate>(params) directly on a string.
    // @nuget: Z.Expressions.Eval
    using Z.Expressions;
    
    // Compile to a custom delegate
    var compiled = Eval.Compile<Func<int, int, int>>("X + Y", "X", "Y");
    int result = compiled(1, 2);
    
    // Using Extension Methods
    string s = "X + Y";
    var compiled2 = s.Compile<Func<int, int, int>>("X", "Y");
    int result2 = compiled2(1, 2);
  6. Eval Expression.NET PRO features

    master

    The PRO version removes limitations found in the free version. Key features include:

    • Unlimited Maximum Characters: No limit on the length of the expression or code being evaluated.
    • Commercial License: Available for commercial use.
    • Royalty-Free: Usage is royalty-free.
    • Support & Upgrades: Includes 1 year of support and upgrades.

    Note: Starting from version 2.x, there is no longer a free limited version. LINQ extension methods are free with EF Classic - Community.