11---
22layout : default
33title : Compile & Execute
4+ description : Compile and execute a C# expression at runtime.
45permalink : compile-execute
56---
67
78{% include template-h1.html %}
89
9- ## Introduction
10-
11- The EvalContext allow specifying customer configuration for evaluating, compiling and executing C# code and expression at runtime.
10+ ## Description
11+ {{ page.description }}
1212
1313## Execute
14- ### Problem
15- You need to evaluate a dynamic C# code in an instance context.
14+ Execute a C# expression and return the result:
1615
17- ### Solution
18- Create a new instance of EvalContext and use Execute method:
1916- Execute< ; TResult> ; (string code)
2017- Execute< ; TResult> ; (string code, object parameters)
2118- Execute< ; TResult> ; (string code, params object[ ] parameters)
@@ -24,22 +21,19 @@ Create a new instance of EvalContext and use Execute method:
2421- Execute(string code, params object[ ] parameters)
2522
2623### Example
27- ``` csharp
24+ {% highlight csharp %}
2825// using Z.Expressions; // Don't forget to include this.
2926
3027var context = new EvalContext();
3128// ... context options ...
3229
3330string code = "Price * Quantity";
3431var price = context.Execute<decimal >(code, orderItem);
35- ```
32+ {% endhighlight %}
3633
3734## Compile
38- ### Problem
39- You need to compile a dynamic C# code in an instance context.
35+ Compile a C# expression and return a delegate:
4036
41- ### Solution
42- Create a new instance of EvalContext and use Compile method:
4337- Compile< ; TDelegate> ; (string code)
4438- Compile< ; TDelegate> ; (string code, IEnumerable< ; string> ; parameterNames)
4539- Compile< ; TDelegate> ; (string code, params string[ ] parameterNames)
@@ -51,7 +45,7 @@ Create a new instance of EvalContext and use Compile method:
5145- Compile(string, IDictionary< ; string, Type> ; ): ** Func< ; IDictionary, object> ; **
5246
5347### Example
54- ``` csharp
48+ {% highlight csharp %}
5549// using Z.Expressions; // Don't forget to include this.
5650
5751var context = new EvalContext();
@@ -65,4 +59,4 @@ foreach(var item in list)
6559{
6660 totals += compiled(item);
6761}
68- ```
62+ {% endhighlight %}
0 commit comments