Expression Tree
Jason
Expression tree
Expression trees represent code in a tree-like data structure, where each node is
an expression.
This enables dynamic modification of executable code, the execution of LINQ
queries in various databases, and the creation of dynamic queries.
Lambda
Statement lambda
(x, y) => { return x + y; }
Expression lambda
(x, y) => x + y
Lambda Expression
Using the System.Linq.Expressions namespace
Expression<Func<int, bool>> expr = n => n > 5;
Expression<TDelegate>.Compile
Compile the expression tree into executable code.
Expression<Func<int, bool>> expr = n => n > 5;
Func<int, bool> deleg = expr.Compile();
deleg(3);
Expression<Func<int, bool>>
n => n > 5
Body : BinaryExpression
n > 5
Left : Expression
n
Right : Expression
5
NodeType : ExpressionType
GreaterThan
NodeType : ExpressionType
Lambda
ReturnType : Type
Boolean
ParameterExpression
ParameterExpression iParam =
Expression.Parameter(typeof(int), "n");
>
n 5
λ
BinaryExpression
BinaryExpression binaryExpr =
Expression.GreaterThan(iParam, constantExpr);
>
n 5
λ
ConstantExpression
ConstantExpression constantExpr =
Expression.Constant(5, typeof(int));
>
n 5
λ
LambdaExpression
Expression<Func<int, bool>> expr =
Expression.Lambda<Func<int, bool>>(binaryExpr, iParam);
>
n 5
λ
Reference
Expression Tree - MSDN
https://msdn.microsoft.com/en-us/library/bb397951.aspx

Expression tree

Editor's Notes

  • #6 https://msdn.microsoft.com/zh-tw/library/bb361179(v=vs.110).aspx