|
| 1 | +Evaluate, Compile and Execute C# code at runtime. |
| 2 | + |
| 3 | +## Url |
| 4 | + |
| 5 | +- [Website](https://eval-expression.net/) |
| 6 | +- [Getting Started](https://eval-expression.net/overview) |
| 7 | +- [Documentation](https://eval-expression.net/eval-execute) |
| 8 | +- [Online Examples](https://eval-expression.net/online-examples) |
| 9 | + |
| 10 | +## Example Eval.Execute |
| 11 | + |
| 12 | +```csharp |
| 13 | +// Parameter: Anonymous Type |
| 14 | +int result = Eval.Execute<int>("X + Y", new { X = 1, Y = 2} ); |
| 15 | + |
| 16 | +// Parameter: Argument Position |
| 17 | +int result = Eval.Execute<int>("{0} + {1}", 1, 2); |
| 18 | + |
| 19 | +// Parameter: Class Member |
| 20 | +dynamic expandoObject = new ExpandoObject(); |
| 21 | +expandoObject.X = 1; |
| 22 | +expandoObject.Y = 2; |
| 23 | +int result = Eval.Execute<int>("X + Y", expandoObject); |
| 24 | + |
| 25 | +// Parameter: Dictionary Key |
| 26 | +var values = new Dictionary<string, object>() { {"X", 1}, {"Y", 2} }; |
| 27 | +int result = Eval.Execute<int>("X + Y", values); |
| 28 | +``` |
| 29 | + |
| 30 | +Try it: [https://dotnetfiddle.net/xquPID](https://dotnetfiddle.net/xquPID) |
| 31 | + |
| 32 | +## Example Eval.Compile |
| 33 | + |
| 34 | +```csharp |
| 35 | +// Delegate Func |
| 36 | +var compiled = Eval.Compile<Func<int, int, int>>("{0} + {1}"); |
| 37 | +int result = compiled(1, 2); |
| 38 | + |
| 39 | +// Delegate Action |
| 40 | +var compiled = Eval.Compile<Action<int, int>>("{0} + {1}"); |
| 41 | +compiled(1, 2); |
| 42 | + |
| 43 | +// Named Parameter |
| 44 | +var compiled = Eval.Compile<Func<int, int, int>>("X + Y", "X", "Y"); |
| 45 | +int result = compiled(1, 2); |
| 46 | +``` |
| 47 | + |
| 48 | +Try it: [https://dotnetfiddle.net/MBHlX8](https://dotnetfiddle.net/MBHlX8) |
0 commit comments