Compile();
var three = originalDelegate.Invoke(2);
Chapter 1
[ 23 ]
Each node in the expression tree represents an expression. If we decompose
the expression, we can find out how the expression tree represents the lambda
expressions. Following is the sample code to decompose the previous expression:
ParameterExpression parameter =
ParameterExpression)expression.Parameters[0];
BinaryExpression operation = (BinaryExpression)expression.Body;
ParameterExpression left = (ParameterExpression)operation.Left;
ConstantExpression right = (ConstantExpression)operation.Right;
Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}",
parameter.Name, left.Name, operation.NodeType, right.Value);
The output of the above decomposition would be:
Decomposed expression: func => func Add 5
Expression trees are implemented in the System.Query.dll assembly under the
System.Linq.Expressions namespace. The abstract class Expression provides
the root of a class hierarchy used to model expression trees. The Expression class
contains static factory methods to create expression tree nodes of various types.
Pages:
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51