@@ -17,23 +17,69 @@ dotnet add package Ramstack.ExpressionParser
1717## Usage
1818
1919``` csharp
20+ using System.Linq.Expressions ;
21+ using Ramstack.Parsing ;
22+
2023var result = ExpressionParser .Parse (" math.min(2 + 3, 2 * 3)" );
2124if (result.Success )
2225{
23- var lambda = Expression .Lambda <Func <int >>(result .Value );
26+ var lambda = Expression .Lambda <Func <int >>(result .Value ! );
2427 var fn = lambda .Compile ();
2528
2629 Console .WriteLine (fn ());
2730}
2831```
2932
33+ If the expression is invalid, ` Success ` is ` false ` , and ` ErrorMessage ` describes the error.
34+
35+ Identifiers are resolved in a case-insensitive manner, so ` math.min ` , ` Math.Min ` , and ` MATH.MIN `
36+ refer to the same method.
37+
38+ ### Evaluating expressions
39+
40+ ` Evaluate ` parses, compiles, and runs an expression in a single call:
41+
42+ ``` csharp
43+ var sum = ExpressionParser .Evaluate <int >(" 2 + 3 * 4" );
44+ var area = ExpressionParser .Evaluate <Circle , double >(" Math.PI * radius * radius" , circle );
45+ ```
46+
47+ The second call evaluates the expression against ` circle ` , so the expression can use its members
48+ without qualification. If the expression is invalid, ` Evaluate ` throws an ` ArgumentException `
49+ that describes the error.
50+
51+ ` Evaluate ` parses and compiles the expression on every call. To evaluate the same expression repeatedly,
52+ use ` Parse ` and compile the resulting expression once.
53+
54+ ### Available types
55+
56+ By default, expressions can use the following types:
57+
58+ - the built-in types ` bool ` , ` byte ` , ` sbyte ` , ` short ` , ` ushort ` , ` int ` , ` uint ` , ` long ` , ` ulong ` , ` nint ` , ` nuint ` ,
59+ ` float ` , ` double ` , ` decimal ` , ` char ` , ` string ` , and ` object ` , also by their .NET names, such as ` Int32 `
60+ and ` Boolean ` ;
61+ - ` Half ` and ` BigInteger ` ;
62+ - ` Convert ` , ` CultureInfo ` , ` DateTime ` , ` DateTimeOffset ` , ` DateOnly ` , ` TimeOnly ` , ` DateTimeKind ` , ` DayOfWeek ` ,
63+ ` Guid ` , ` Math ` , ` MathF ` , ` Regex ` , ` RegexOptions ` , ` StringComparer ` , ` StringComparison ` , ` StringSplitOptions ` ,
64+ ` TimeSpan ` , ` TimeZoneInfo ` , and ` Version ` .
65+
66+ Other types are registered with ` RegisterType ` under their simple name, without the namespace:
67+
68+ ``` csharp
69+ var binder = new DefaultBinder ();
70+ binder .RegisterType (typeof (Path ));
71+
72+ var result = ExpressionParser .Parse (" Path.GetFileName(\" logs/app.txt\" )" , binder );
73+ ```
74+
75+ With ` importAsStatic: true ` , the static members of the type can also be used without the type name,
76+ as with a ` using static ` directive, for example ` GetFileName("logs/app.txt") ` .
77+
3078### Using ` ContextAwareBinder `
3179
3280` ContextAwareBinder ` allows binding expressions to a specific context,
3381making it possible to reference its properties, fields, and methods directly within expressions.
34-
35- - The provided context acts as an implicit ** this** , meaning you can access its members without qualification.
36- - Identifiers are resolved in a case-insensitive manner, so ` level ` , ` Level ` , and ` LEVEL ` are treated the same.
82+ The provided context acts as an implicit ** this** , meaning you can access its members without qualification.
3783
3884``` csharp
3985var parameter = Expression .Parameter (typeof (LogEvent ), " logEvent" );
@@ -42,7 +88,7 @@ binder.RegisterType(typeof(LogLevel));
4288
4389var result = ExpressionParser .Parse (" level == LogLevel.Error && string.IsNullOrEmpty(source)" , binder );
4490_predicate = Expression
45- .Lambda <Predicate <LogEvent >>(result.Value , parameter )
91+ .Lambda <Predicate <LogEvent >>(result.Value ! , parameter )
4692 .Compile ();
4793```
4894
0 commit comments