using System;02 | using 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 | string 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 | DisplayTree(indent, "Body", lambdaExpr.Body); |
24 | break; |
25 | case ExpressionType.Constant: |
26 | ConstantExpression constExpr = (ConstantExpression)expression; |
27 | Console.WriteLine("{0} Const Value: {1}", output, constExpr.Value); |
28 | break; |
29 | case ExpressionType.Parameter: |

Join the conversation! Your thoughts help the community grow.