https://github.com/rameel/ramstack.expressionparser
A flexible expression parser library for .NET, allowing dynamic evaluation and binding of expressions with context-aware support.
https://github.com/rameel/ramstack.expressionparser
expression-parser parser
Last synced: 7 months ago
JSON representation
A flexible expression parser library for .NET, allowing dynamic evaluation and binding of expressions with context-aware support.
- Host: GitHub
- URL: https://github.com/rameel/ramstack.expressionparser
- Owner: rameel
- License: mit
- Created: 2025-02-20T14:43:16.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-05-24T18:53:37.000Z (8 months ago)
- Last Synced: 2025-05-24T19:08:29.236Z (8 months ago)
- Topics: expression-parser, parser
- Language: C#
- Homepage:
- Size: 68.4 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ramstack.ExpressionParser
[](https://nuget.org/packages/Ramstack.ExpressionParser)
[](https://github.com/rameel/ramstack.expressionparser/blob/main/LICENSE)
**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](https://www.nuget.org/packages/Ramstack.ExpressionParser) to your project, run the following command:
```shell
dotnet add package Ramstack.ExpressionParser
```
## Usage
```csharp
var result = ExpressionParser.Parse("math.min(2 + 3, 2 * 3)");
if (result.Success)
{
var lambda = Expression.Lambda>(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).
```csharp
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>(result.Value, parameter)
.Compile();
```
Here, `IsEnabled` evaluates the parsed expression against a `LogEvent` instance:
```csharp
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](https://github.com/rameel/ramstack.expressionparser/blob/main/LICENSE) file for details.