Z.Expressions.Eval 6.1.4

Z.Expressions.Eval: C# Eval Expression

Z.Expressions.Eval is a robust NuGet package that enables .NET developers to evaluate, compile, and execute dynamic C# expressions. By interpreting and processing strings as executable code, this package empowers developers to craft highly adaptable and customizable applications.

It has the capability to handle an extensive range of mathematical operations, string manipulations, boolean logic, and it can even interact with .NET objects and classes.

The principal advantage of Z.Expressions.Eval lies in its ability to provide enhanced flexibility by facilitating runtime execution and manipulation of C# expressions. This feature is particularly beneficial in scenarios where business rules are subject to frequent changes or need to be configurable.

Having earned the trust and endorsement of over 5000 customers worldwide, C# Eval Expression is a highly regarded library that has become an essential tool for a wide variety of projects.

Resources:

  • Official Website: Visit for comprehensive information, updates, tutorials, and more. Learn how Z.Expressions.Eval can significantly enhance your application's capabilities.
  • Contact Us: Have a question or need assistance? Don't hesitate to reach out. We're here to help you get the most out of C# Eval Expression.
  • GitHub Issues: Encountered an issue or have a feature request? Let us know on GitHub. Your feedback helps us make C# Eval Expression even better.

Supported .NET Versions

Z.Expressions.Eval exhibits broad compatibility with a variety of .NET versions. Starting from .NET Framework 4.5 and .NET Core 2.0, it extends support to the most recent versions of .NET.

To take full advantage of the enhancements in newer .NET versions, we highly recommend upgrading to the latest package version of Z.Expressions.Eval.

Main Features

Z.Expressions.Eval provides a set of powerful features designed to simplify dynamic C# expression evaluation and execution. Here are some of the key features:

  • Eval.Execute: Allows you to evaluate and execute a string as a C# expression at runtime. This feature can handle a wide variety of mathematical operations, string manipulations, boolean logic, and even work with .NET objects and classes.
  • Eval.Compile: Offers the ability to compile a string as a C# expression at runtime, reducing the overhead of repeated evaluations. This feature is especially useful when working with expressions that need to be executed multiple times.
  • LINQ Dynamic: Provides the power to execute LINQ expressions dynamically. This feature opens up new possibilities for creating highly flexible and configurable queries.

For a more detailed explanation and examples of each operation, please refer to the official documentation page.

Getting Started

To get a feel for Z.Expressions.Eval, here is a simple example:

using Z.Expressions;

public class Program
{
    public static void Main()
    {
        var result = Eval.Execute<int>("X*Y", new { X = 10, Y = 20 });
        Console.WriteLine(result); // Outputs: 200
    }
}

In the above example, the "X*Y" string expression is evaluated dynamically at runtime with the values of X and Y provided in an anonymous object. The result is then printed to the console.

Want to explore the library further? We offer an extensive collection of online examples showcasing the various functionalities and capabilities of C# Eval Expression:

These examples are specifically designed to impart practical understanding of C# Eval Expression, demonstrating its potent features and adaptable options in a variety of scenarios.

Advanced Usage

Instance Context

Z.Expressions.Eval allows more advanced scenarios, such as operating in a specific context, using variables, and more. Here's an example illustrating the use of instance context:

// CREATE a new instance of EvalContext
var context = new EvalContext();

// USE the `Execute` method from the context created
var list1 = context.Execute<List<int>>("new List<int>() { 1, 2, 3 };");

In this example, we first create a new instance of EvalContext. Then, we use the Execute method from the created context to evaluate and execute a string as a C# expression, which instantiates a new List<int>.

Try it online

Register Type

The RegisterType method allows you to register all types provided for the context. The method also registers extension methods from those types but does not register other static members. For instance, if you register the type "Z.MyNamespace.MyClass", you can subsequently create an expression such as "new MyClass()" or any extension methods from this type.

In the following example, we'll register the types MyClassHelper and MyExtensions to include their extension methods. We'll first demonstrate how to use our class and extension methods in an arithmetic operation. We'll then show how to utilize the IsRegisteredType and UnregisterType methods with MyClassHelper to verify if the type is currently registered and to unregister it.

using System;
using System.Collections.Generic;
using Z.Expressions;

public class Program
{
	public static void Main()
	{
		// Global Context: EvalManager.DefaultContext.RegisterType(typeof(Helper));
		
		var context = new EvalContext();
		context.RegisterType(typeof(MyClassHelper));
		context.RegisterType(typeof(MyExtensions));
		
		var r1 = context.Execute<int>("new MyClassHelper().MyClassHelperID + 2.AddMe(3)"); // return 6
		Console.WriteLine("1 - Result: " + r1);
		
		// Check if the type `MyClassHelper` is registered
		var r2 = context.IsRegisteredType(typeof(MyClassHelper)); // return true
		Console.WriteLine("2 - Result: " + r2);
		
		// Unregister the type `MyClassHelper`
		context.UnregisterType(typeof(MyClassHelper));
		
		// Check the type `MyClassHelper` has been succesfully unregistered
		var r3 = context.IsRegisteredType(typeof(MyClassHelper)); // return false
		Console.WriteLine("3 - Result: " + r3);
	}
	
	public class MyClassHelper
	{
		public int MyClassHelperID { get; set; } = 1;
	}
}

public static class MyExtensions
{	
	public static int AddMe(this int x, int y)
	{
		return x + y;
	}
}

Try it online

Use Options

The SafeMode option allows you to set the context to only allow the usage of registered members and types within expressions. In essence, it enables a secure execution environment for user input, restricting usage to only what you explicitly permit. By default, SafeMode is set to false.

In this example, we demonstrate how SafeMode method. Please ensure to thoroughly understand the SafeMode documentation if you intend to use this option.

// Global Context: EvalManager.DefaultContext.SafeMode = true;

var context = new EvalContext();

context.SafeMode = true;
context.UnregisterAll();

try
{
	var fail = context.Execute("Math.Min(1, 2)");
	Console.WriteLine(fail);
}
catch(Exception ex)
{
	Console.WriteLine("1 - Exception: " + ex.Message);
}

// try again by registering "System.Math" member
context.RegisterMember(typeof(System.Math));
var r2 = context.Execute("Math.Min(1, 2)");
Console.WriteLine("2 - Result: " + r2);

Try it online

Release Notes

For a thorough record of enhancements, bug fixes, and upgrades in each iteration of C# Eval Expression, we recommend referring to the official Release Notes located in our GitHub repository.

The Release Notes offer essential insights about each version, detailing new features, addressing resolved issues, and mentioning any breaking changes (if applicable). We strongly advise reviewing these notes prior to upgrading to a newer version. This practice not only ensures you leverage the full potential of the newly introduced features but also helps prevent unforeseen complications.

License

C# Eval Expression utilizes a paid licensing model. To acquire a license, please visit our official Pricing Page on the Eval Expression website.

Showing the top 20 packages that depend on Z.Expressions.Eval.

Packages Downloads
Z.EntityFramework.Plus.EFCore
EF+ Full Version | C# Entity Framework Utilities (Bulk Operations | Batch Delete | Batch Update | Query Cache | Query Filter | Query Future | Query Include | Audit) **IMPORTANT** - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
16
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
3
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
7
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
11
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
22
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
4
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
92
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 8.x, use the latest EF Plus v8.x version - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
3
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 8.x, use the latest EF Plus v8.x version - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
4
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 8.x, use the latest EF Plus v8.x version - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
5
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 8.x, use the latest EF Plus v8.x version - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
6
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 8.x, use the latest EF Plus v8.x version - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
8
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 8.x, use the latest EF Plus v8.x version - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
109
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 8.x, use the latest EF Plus v8.x version - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
165
Z.EntityFramework.Plus.EFCore
Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more **IMPORTANT** - For EF Core 8.x, use the latest EF Plus v8.x version - For EF Core 7.x, use the latest EF Plus v7.x version - For EF Core 6.x, use the latest EF Plus v6.x version - For EF Core 5.x, use the latest EF Plus v5.x version - For EF Core 3.x, use the latest EF Plus v3.x version - For EF Core 2.x, use the latest EF Plus v2.x version
187

Version Downloads Last updated
6.1.4 98 05/24/2024
6.1.3 131 05/13/2024
6.1.2 5 05/30/2024
6.1.1 1 06/01/2024
6.1.0 4 05/30/2024
6.0.6 2 05/30/2024
6.0.5 1 06/01/2024
6.0.4 5 05/30/2024
6.0.3 2 05/30/2024
6.0.2 2 06/01/2024
6.0.1 1 06/01/2024
6.0.0 3 06/01/2024
5.0.12 1 06/01/2024
5.0.11 2 05/30/2024
5.0.10 2 06/01/2024
5.0.9 1 06/01/2024
5.0.8 2 05/30/2024
5.0.7 61 08/08/2023
5.0.6 2 06/22/2023
5.0.5 3 06/22/2023
5.0.4 1 06/01/2024
5.0.3 2 06/01/2024
5.0.2 3 04/14/2023
5.0.1 3 05/30/2024
5.0.0 1 06/01/2024
4.0.92 2 06/01/2024
4.0.91 1 06/01/2024
4.0.90 1 06/01/2024
4.0.89 18 10/14/2022
4.0.88 2 06/01/2024
4.0.87 3 06/01/2024
4.0.86 1 06/01/2024
4.0.85 1 06/01/2024
4.0.84 1 06/01/2024
4.0.83 2 05/30/2024
4.0.82 1 06/01/2024
4.0.81 2 06/01/2024
4.0.80 1 06/01/2024
4.0.78 1 06/01/2024
4.0.77 2 06/01/2024
4.0.73 1 06/01/2024
4.0.72 1 06/01/2024
4.0.69 1 06/01/2024
4.0.67 1 06/01/2024
4.0.66 1 06/01/2024
4.0.63 1 06/01/2024
4.0.61 1 06/01/2024
4.0.58 1 06/01/2024
4.0.57 4 04/26/2022
4.0.56 1 06/01/2024
4.0.55 1 06/01/2024
4.0.54 1 06/01/2024
4.0.52 1 06/01/2024
4.0.50 1 06/01/2024
4.0.47 1 06/01/2024
4.0.46 1 06/01/2024
4.0.45 2 06/01/2024
4.0.44 2 04/26/2022
4.0.43 4 06/01/2024
4.0.41 3 06/01/2024
4.0.40 2 06/01/2024
4.0.39 1 06/01/2024
4.0.38 1 06/01/2024
4.0.36 1 06/01/2024
4.0.35 1 06/01/2024
4.0.33 1 06/01/2024
4.0.30 1 06/01/2024
4.0.29 1 06/01/2024
4.0.27 1 06/01/2024
4.0.26 1 06/01/2024
4.0.25 3 04/26/2022
4.0.20 1 06/01/2024
4.0.19 3 06/01/2024
4.0.18 2 06/01/2024
4.0.17 2 06/01/2024
4.0.16 2 06/01/2024
4.0.15 1 06/01/2024
4.0.14 4 06/01/2024
4.0.12 2 06/01/2024
4.0.11 1 06/01/2024
4.0.9 1 06/01/2024
4.0.8 1 06/01/2024
4.0.7 3 06/01/2024
4.0.5 1 06/01/2024
4.0.4 1 06/01/2024
4.0.3 2 06/01/2024
4.0.2 3 06/01/2024
4.0.1 4 06/01/2024
4.0.0 1 06/01/2024
3.1.12 4 06/01/2024
3.1.11 1 06/01/2024
3.1.7 2 04/28/2024
3.1.6 1 06/01/2024
3.1.4 1 06/01/2024
3.1.3 4 06/01/2024
3.1.2 4 06/01/2024
3.1.1 1 06/01/2024
3.1.0 3 06/01/2024
3.0.14 1 06/01/2024
3.0.13 4 06/01/2024
3.0.12 4 06/01/2024
3.0.8 4 06/01/2024
3.0.7 1 06/01/2024
3.0.5 1 06/01/2024
3.0.4 6 06/01/2024
3.0.3 1 06/01/2024
3.0.2 3 06/01/2024
3.0.1 1 06/01/2024
2.9.57 4 06/01/2024
2.9.56 1 06/01/2024
2.9.54 1 06/01/2024
2.9.51 1 06/01/2024
2.9.50 2 06/01/2024
2.9.49 1 06/01/2024
2.9.48 1 06/01/2024
2.9.46 1 06/01/2024
2.9.44 1 06/01/2024
2.9.41 1 06/01/2024
2.9.40 1 06/01/2024
2.9.39 2 06/01/2024
2.9.36 1 06/01/2024
2.9.33 1 06/01/2024
2.9.31 3 06/01/2024
2.9.29 3 06/01/2024
2.9.28 1 06/01/2024
2.9.27 2 06/01/2024
2.9.26 3 06/01/2024
2.9.25 4 06/01/2024
2.9.24 4 06/01/2024
2.9.22 1 06/01/2024
2.9.21 1 06/01/2024
2.9.17 1 06/01/2024
2.9.16 1 06/01/2024
2.9.15 1 06/01/2024
2.9.13 1 06/01/2024
2.9.11 1 06/01/2024
2.9.7 1 06/01/2024
2.9.6 1 06/01/2024
2.9.5 2 06/01/2024
2.9.4 3 06/01/2024
2.9.2 2 06/01/2024
2.9.0 1 06/01/2024
2.8.9 2 06/01/2024
2.8.7 2 05/02/2024
2.8.6 1 06/01/2024
2.8.5 2 06/01/2024
2.8.3 3 06/01/2024
2.8.2 2 06/01/2024
2.8.1 1 06/01/2024
2.8.0 2 06/01/2024
2.7.13 1 06/01/2024
2.7.12 4 06/01/2024
2.7.9 1 06/01/2024
2.7.4 1 06/01/2024
2.7.2 3 06/01/2024
2.7.0 1 06/01/2024
2.6.3 1 06/01/2024
2.6.2 2 05/15/2024
2.6.1 2 04/26/2022
2.6.0 2 05/15/2024
2.5.0 1 06/01/2024
2.4.21 1 06/01/2024
2.4.18 1 05/28/2024
2.4.17 1 05/28/2024
2.4.16 3 05/28/2024
2.4.12 4 05/28/2024
2.4.10 1 05/28/2024
2.4.8 3 06/01/2024
2.4.7 2 06/01/2024
2.4.6 1 06/01/2024
2.4.5 1 06/01/2024
2.4.4 1 06/01/2024
2.4.1 1 06/01/2024
2.4.0 1 06/01/2024
2.2.1 1 06/01/2024
2.2.0-beta1 1 05/28/2024
2.1.14 1 05/28/2024
2.1.11 1 05/28/2024
2.1.10 1 05/28/2024
2.1.9 1 06/01/2024
2.1.8 1 06/01/2024
2.1.7 1 06/01/2024
2.1.6 4 06/01/2024
2.1.4 1 06/01/2024