2. What is Expression Trees Is a Tree Has 56 Expressions Add, Bind, Block, Break, Call, Catch, Coalesce, Continue, Equal, GreaterThan, Lambda, Loop, New, Or , PowerAssign …………… Expression: 5 + 4 * 3 + 5 * 4 3
3. How to Build? Func<int, int> myDelegate = (a, b) => a + b; Expression<Func<int, int>> myExpression = (a, b) => a + b; BinaryExpressionmyExpression = Expression.Add( Expression.Constant( 2 ), Expression.Constant( 5 ) );
4. How to Use? BinaryExpressionmyExpression = Expression.Add( Expression.Constant( 2 ), Expression.Constant( 5 ) ); var compiled = Expression.Lambda<Func<int>>( myExpression ).Compile(); compiled(); Expression<Func<int, bool>> expr = num => num < 5; Func<int, bool> compiled = expr.Compile(); compiled (4);
5. Where can be useful? Dynamic code; Object cloning; Alternative for reflection; Transform a domain language into executable code; Patterns code improvement.