Ramstack.ExpressionParser 1.0.0-beta.2

This is a prerelease version of Ramstack.ExpressionParser.
dotnet add package Ramstack.ExpressionParser --version 1.0.0-beta.2                
NuGet\Install-Package Ramstack.ExpressionParser -Version 1.0.0-beta.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ramstack.ExpressionParser" Version="1.0.0-beta.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ramstack.ExpressionParser --version 1.0.0-beta.2                
#r "nuget: Ramstack.ExpressionParser, 1.0.0-beta.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ramstack.ExpressionParser as a Cake Addin
#addin nuget:?package=Ramstack.ExpressionParser&version=1.0.0-beta.2&prerelease

// Install Ramstack.ExpressionParser as a Cake Tool
#tool nuget:?package=Ramstack.ExpressionParser&version=1.0.0-beta.2&prerelease                

Ramstack.ExpressionParser

NuGet MIT

Ramstack.ExpressionParser is a flexible expression parser library for .NET, allowing dynamic evaluation and binding of expressions with context-aware support.

Getting Started

To install the Ramstack.ExpressionParser NuGet package to your project, run the following command:

dotnet add package Ramstack.ExpressionParser

Usage

var result = ExpressionParser.Parse("math.min(2 + 3, 2 * 3)");
if (result.Success)
{
    var lambda = Expression.Lambda<Func<int>>(result.Value);
    var fn = lambda.Compile();

    Console.WriteLine(fn());
}

Using ContextAwareBinder

ContextAwareBinder allows binding expressions to a specific context, making it possible to reference its properties, fields, and methods directly within expressions.

  • The provided context acts as an implicit this, meaning you can access its members without prefixes.
  • Case-insensitive binding: identifiers in expressions are resolved in case-insensitive manner (e.g., level, Level, and LEVEL are treated the same).
var parameter = Expression.Parameter(typeof(LogEvent), "logEvent");
var binder = new ContextAwareBinder(parameter);

var result = ExpressionParser.Parse("level == LogLevel.Error && string.IsNullOrEmpty(source)", binder);
var predicate = Expression
    .Lambda<Predicate<LogEvent>>(result.Value, parameter)
    .Compile();

Here, IsEnabled evaluates the parsed expression against a LogEvent instance:

public bool IsEnabled(LogEvent logEvent)
{
    return _predicate(logEvent);
}

This makes it easy to create dynamic, human-readable expressions for filtering or evaluating objects at runtime.

Supported Versions

Version
.NET 6, 7, 8, 9

Contributions

Bug reports and contributions are welcome.

License

This package is released under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-beta.2 60 2/21/2025
1.0.0-beta.1 60 2/20/2025