From 91e834645f782fa8074a53298c9f9f360f4bff04 Mon Sep 17 00:00:00 2001 From: rameel Date: Thu, 19 Nov 2026 16:39:10 +0500 Subject: [PATCH] docs: fix the ContextAwareBinder example in README The example compared level with LogLevel.Error without registering LogLevel, so parsing failed with "Cannot resolve symbol 'LogLevel'", and IsEnabled used a _predicate field that the example never set. The example now registers LogLevel and assigns the compiled predicate to _predicate. It also lists the supported versions as .NET 6 and later, since the package targets net6.0, links CONTRIBUTING.md, and rewords a few sentences. Co-Authored-By: Claude Opus 5.5 --- README.md | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 45c558a..a0a8844 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,13 @@ [![NuGet](https://img.shields.io/nuget/v/Ramstack.ExpressionParser.svg)](https://nuget.org/packages/Ramstack.ExpressionParser) [![MIT](https://img.shields.io/github/license/rameel/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. +**Ramstack.ExpressionParser** is a .NET library that parses expressions written in a C#-like syntax +into `System.Linq.Expressions` trees, which you can compile and evaluate, +optionally against a context object. ## Getting Started -To install the `Ramstack.ExpressionParser` [NuGet package](https://www.nuget.org/packages/Ramstack.ExpressionParser) to your project, run the following command: +To add 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 @@ -31,20 +32,23 @@ if (result.Success) `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). +- The provided context acts as an implicit **this**, meaning you can access its members without qualification. +- Identifiers are resolved in a case-insensitive manner, so `level`, `Level`, and `LEVEL` are treated the same. ```csharp var parameter = Expression.Parameter(typeof(LogEvent), "logEvent"); var binder = new ContextAwareBinder(parameter); +binder.RegisterType(typeof(LogLevel)); var result = ExpressionParser.Parse("level == LogLevel.Error && string.IsNullOrEmpty(source)", binder); -var predicate = Expression +_predicate = Expression .Lambda>(result.Value, parameter) .Compile(); ``` -Here, `IsEnabled` evaluates the parsed expression against a `LogEvent` instance: +Types that are not available by default, such as `LogLevel` here, are registered with `RegisterType`. + +`IsEnabled` then evaluates the compiled predicate against a `LogEvent` instance: ```csharp public bool IsEnabled(LogEvent logEvent) { @@ -60,14 +64,15 @@ Use the `:>` operator to convert a value to a type, for example `count :> long` A conversion binds more tightly than prefix and binary operators, so `-x :> int` means `-(x :> int)`, and `x :> long + 1` means `(x :> long) + 1`. -Earlier versions wrote conversions as `x:int`. That syntax conflicted with the conditional operator, -so expressions such as `flag ? 0 : count` failed to parse. It is no longer supported; -replace `x:int` with `x :> int`. +In earlier versions, conversions were written as `x:int`. That syntax conflicted +with the conditional operator, so expressions such as `flag ? 0 : count` failed to parse. +It is no longer supported; replace `x:int` with `x :> int`. ### Limits An expression tree can be at most 256 levels deep. Each operator, member access, call, indexer, -and pair of parentheses adds a level, so a chain such as `a + b + c` gets one level deeper with each operator. +and pair of parentheses adds a level, so a chain such as `a + b + c` gets one level deeper +with each operator. Parsing fails with an error rather than crashing the process when an expression is deeper than that, or when it is nested too deeply to fit on the stack of the current thread. @@ -76,13 +81,14 @@ so limit the length of expressions that come from untrusted sources. ## Supported Versions -| | Version | -|------|------------| -| .NET | 6, 7, 8, 9 | +| | Version | +|------|-------------| +| .NET | 6 and later | ## Contributions Bug reports and contributions are welcome. +See [CONTRIBUTING.md](https://github.com/rameel/ramstack.expressionparser/blob/main/CONTRIBUTING.md) for details. ## License