.NET 개발/C#
lambda expression
Hoya0415
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<int, int, bool>> 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 |