C# Eval Expression Compile & Execute Methods
Description
Compile and execute a C# expression at runtime.
Execute
Execute a C# expression and return the result:
- Execute<TResult>(string code)
- Execute<TResult>(string code, object parameters)
- Execute<TResult>(string code, params object[] parameters)
- Execute(string code)
- Execute(string code, object parameters)
- Execute(string code, params object[] parameters)
Example
// using Z.Expressions; // Don't forget to include this. var context = new EvalContext(); // ... context options ... string code = "Price * Quantity"; var price = context.Execute<decimal>(code, orderItem);
Compile
Compile a C# expression and return a delegate:
- Compile<TDelegate>(string code)
- Compile<TDelegate>(string code, IEnumerable<string> parameterNames)
- Compile<TDelegate>(string code, params string[] parameterNames)
- Compile(string): Func<object>
- Compile(string, Type type1): Func<object, object>
- Compile(string, Type type1, ... , Type type9): Func<object, ... , object, object>
- Compile(string, IEnumerable<Type>): Func<IEnumerable, object>
- Compile(string, params Type[]): Func<IEnumerable, object>
- Compile(string, IDictionary<string, Type>): Func<IDictionary, object>
Example
// using Z.Expressions; // Don't forget to include this. var context = new EvalContext(); // ... context options ... string code = "Price * Quantity"; var compiled = context.Compile<Func<OrderItem, decimal>>(code); decimal totals = 0; foreach(var item in list) { totals += compiled(item); }
ZZZ Projects