Skip to content

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

License

Notifications You must be signed in to change notification settings

rameel/ramstack.expressionparser

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages