lambda expression

.NET 개발/C# 2016. 6. 5. 15:36

식 트리가 데이터의 컬렉션이며 반복처리를 거쳐 다른 형태의 자료로 변환 할 수 있다는 점이 중요하다.

식 트리를 자기 기술적 문자열로 변환해서 다른 질의 언어로 변환하는 것도 얼마든지 가능하다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Program
    {
        static void Main(string[] args)
        {
            Expression<Func<intintbool>> expression;
 
            expression = (x, y) => x > y;
 
            Console.WriteLine("--------------{0}---------------",expression);
 
            PrintNode(expression.Body, 0);
 
            Console.WriteLine();
            Console.WriteLine();
 
            expression = (x, y) => x * y > x + y;
 
            Console.WriteLine("--------------{0}---------------", expression);
 
            PrintNode(expression.Body, 0);
 
            Console.WriteLine();
            Console.WriteLine();
        }
 
        private static void PrintNode(Expression expression, int indent)
        {
            if (expression is BinaryExpression)
                PrintNode(expression as BinaryExpression, indent);
            else
                PrintSingle(expression, indent);
        }
 
        private static void PrintNode(BinaryExpression expression, int indent)
        {
            PrintNode(expression.Left, indent + 1);
            PrintSingle(expression, indent);
            PrintNode(expression.Right, indent + 1);
 
 
        }
 
        private static void PrintSingle(Expression expression, int indent)
        {
            Console.WriteLine("{0," + indent * 5 + "}{1}""", NodeToString(expression));
        }
 
        private static string NodeToString(Expression expression)
        {
            switch (expression.NodeType)
            {
                case ExpressionType.Multiply:
                    return  "*";
                case ExpressionType.Add:
                    return "+";
                case ExpressionType.Divide:
                    return "/";
                case ExpressionType.Subtract:
                    return "-";
                case ExpressionType.GreaterThan:
                    return ">";
                case ExpressionType.LessThan:
                    return "<";
                default : return expression.ToString() + " (" + expression.NodeType.ToString() + ")";
            }
        }
    }
cs


'.NET 개발 > C#' 카테고리의 다른 글

C#의 발전 내용  (0) 2016.06.06
대리자와 람다 식  (0) 2016.06.05
공변성, 반공변성  (0) 2016.06.05
Xaml web browser application .xbap 캐시 지우기  (0) 2015.12.21
WebSecurity 클래스  (0) 2015.12.03
Posted by Hoya0415
,