How to use Expression Tree in .Net 3.5

The below blog explains on how the use of Expression Tree in .net 3.5 which is self explanatory!!

using System;
02using System.Linq.Expressions;
03 
04namespace 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:
30                    ParameterExpression paramExpr = (ParameterExpression)expression;
31                    Console.WriteLine("{0} Param Type: {1}", output, paramExpr.Type.Name);
32                    break;
33                case ExpressionType.Equal:
34                case ExpressionType.AndAlso:
35                case ExpressionType.GreaterThan:
36                    BinaryExpression binExpr = (BinaryExpression)expression;
37                    if (binExpr.Method != null)
38                    {
39                        Console.WriteLine("{0} Method: {1}", output, binExpr.Method.Name);
40                    }
41                    else
42                    {
43                        Console.WriteLine(output);
44                    }
45                    DisplayTree(indent, "Left", binExpr.Left);
46                    DisplayTree(indent, "Right", binExpr.Right);
47                    break;
48                case ExpressionType.MemberAccess:
49                    MemberExpression memberExpr = (MemberExpression)expression;
50                    Console.WriteLine("{0} Member Name: {1}, Type: {2}", output,
51                       memberExpr.Member.Name, memberExpr.Type.Name);
52                    DisplayTree(indent, "Member Expr", memberExpr.Expression);
53                    break;
54                default:
55                    Console.WriteLine();
56                    Console.WriteLine("{0} {1}", expression.NodeType, expression.Type.Name);
57                    break;
58            }
59        }
60 
61 
62        static void 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}

Ebook Download
View all
Learn
View all