using
System;
02
System.Linq.Expressions;
03
04
namespace
Sample
05
{
06
class
Program
07
08
private
static
void
DisplayTree(
int
indent,
string
message, Expression expression)
09
10
output = String.Format(
"{0} {1} ! NodeType: {2}; Expr: {3} "
,
11
""
.PadLeft(indent,
'>'
), message, expression.NodeType, expression);
12
13
indent++;
14
switch
(expression.NodeType)
15
16
case
ExpressionType.Lambda:
17
Console.WriteLine(output);
18
LambdaExpression lambdaExpr = (LambdaExpression)expression;
19
foreach
(var parameter
in
lambdaExpr.Parameters)
20
21
DisplayTree(indent,
"Parameter"
, parameter);
22
}
23
"Body"
, lambdaExpr.Body);
24
break
;
25
ExpressionType.Constant:
26
ConstantExpression constExpr = (ConstantExpression)expression;
27
Console.WriteLine(
"{0} Const Value: {1}"
, output, constExpr.Value);
28
29
ExpressionType.Parameter:
30
ParameterExpression paramExpr = (ParameterExpression)expression;
31
"{0} Param Type: {1}"
, output, paramExpr.Type.Name);
32
33
ExpressionType.Equal:
34
ExpressionType.AndAlso:
35
ExpressionType.GreaterThan:
36
BinaryExpression binExpr = (BinaryExpression)expression;
37
if
(binExpr.Method !=
null
)
38
39
"{0} Method: {1}"
, output, binExpr.Method.Name);
40
41
else
42
43
44
45
"Left"
, binExpr.Left);
46
"Right"
, binExpr.Right);
47
48
ExpressionType.MemberAccess:
49
MemberExpression memberExpr = (MemberExpression)expression;
50
"{0} Member Name: {1}, Type: {2}"
, output,
51
memberExpr.Member.Name, memberExpr.Type.Name);
52
"Member Expr"
, memberExpr.Expression);
53
54
default
:
55
Console.WriteLine();
56
"{0} {1}"
, expression.NodeType, expression.Type.Name);
57
58
59
60
61
62
Main()
63
64
Expression<Func<Racer,
bool
>> expression = r => r.Country ==
"UK"
&& r.Wins > 6;
65
66
DisplayTree(0,
"Lambda"
, expression);
67
68
69
70
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: