[C #] Expression Tree,

Source: Internet
Author: User

[C #] Expression Tree,
Expression Tree-Expression Trees order

Let's simply describe the Expression Tree.

 

Directory

 

Introduction

The Expression Tree uses a tree-like Data Structure to represent code. Each node is an expression, such as a method call or a binary operation such as x <y.

You can edit and operate the code in the Expression Tree. In this way, you can dynamically modify the executable code, execute LINQ queries in different databases, and create dynamic queries.

The expression tree can also be used for Dynamic Language Runtime (DLR) to provide dynamic languages and.. NET Framework. It also ensures that compiler writers can transmit expression trees rather than Microsoft intermediate languages (MSIL ).

 

1. Create an Expression Tree Based on Lambda expressions

If a lambda Expression is assigned to a variable of the Expression <TDelegate> type, the compiler can emit code to create an Expression Tree that represents the lambda Expression.

C # the compiler can only generate an expression tree from the expression lambda (or single line lambda.

The following code example uses the keyword Expression to create a lambda Expression:

1             Expression<Action<int>> actionExpression = n => Console.WriteLine(n);2             Expression<Func<int, bool>> funcExpression1 = (n) => n < 0;3             Expression<Func<int, int, bool>> funcExpression2 = (n, m) => n - m == 0;

 

2. Create an expression tree through API

To create an Expression tree using APIs, you must use the Expression class.

The following code example shows how to use an API to create a lambda expression: num => num = 0

1 // create Expression Tree through Expression class 2 // lambda: num => num = 03 ParameterExpression pExpression = Expression. parameter (typeof (int); // Parameter: num4 ConstantExpression cExpression = Expression. constant (0); // Constant: 05 BinaryExpression bExpression = Expression. makeBinary (ExpressionType. equal, pExpression, cExpression); // Binary Expression: num = 06 Expression <Func <int, bool> lambda = Expression. lambda <Func <int, bool> (bExpression, pExpression); // lambda expression: num => num = 0

The code is created using the static method of the Expression class.

 

Iii. parsing Expression Tree

The following code example shows how to break down the Expression Tree that represents the lambda expression num => num = 0.

1 Expression <Func <int, bool> funcExpression = num => num = 0; 2 3 // start parsing 4 ParameterExpression pExpression = funcExpression. parameters [0]; // lambda expression parameter 5 BinaryExpression body = (BinaryExpression) funcExpression. body; // lambda expression Body: num = 06 7 Console. writeLine ($ "Resolution: {pExpression. name }=> {body. left} {body. nodeType} {body. right }");

1 // create Expression Tree 2 Expression <Func <string, int> funcExpression = msg => msg. length; 3 // The Expression Tree is compiled into 4 var lambda = funcExpression. compile (); 5 // call delegate 6 Console. writeLine (lambda ("Hello, World! "); 7 8 // syntax simplified 9 Console. WriteLine (funcExpression. Compile () (" Hello, World! "));

1 const int n = 1; 2 const int m = 2; 3 4 // Expression tree to be executed 5 BinaryExpression bExpression = Expression. add (Expression. constant (n), Expression. constant (m); 6 // create a lambda Expression 7 Expression <Func <int> funcExpression = Expression. lambda <Func <int> (bExpression); 8 // compile lambda expression 9 Func <int> func = funcExpression. compile (); 10 11 // execute lambda expression 12 Console. writeLine ($ "{n} + {m} = {func ()}");

 

7. How to: Modify the Expression Tree

This class inherits the ExpressionVisitor class and is used to modify the expression conditions.ANDThe operation expression. It extracts these operations from the conditionANDChange to ConditionOR. Therefore, this class will override the VisitBinary method of the base type because of the conditionANDThe expression is a binary expression. In the VisitBinary method, if the expression passed to this method represents a conditionANDOperation. The Code constructs an inclusion condition.OROperator (not a condition)ANDOperator. If the expression passed to VisitBinary does not represent a conditionANDThe method is processed by the base class implementation. The base class method constructs a node similar to the input expression tree, but these nodes replace its subdirectory tree with the expression tree generated by the accesser recursively.

1 internal class Program 2 {3 private static void Main (string [] args) 4 {5 Expression <Func <int, bool> funcExpression = num => num = 0; 6 Console. writeLine ($ "Source: {funcExpression}"); 7 8 var visitor = new NotEqualExpressionVisitor (); 9 var expression = visitor. visit (funcExpression); 10 11 Console. writeLine ($ "Modify: {expression}"); 12 13 Console. read (); 14} 15 16 /// <summary> 17 // unequal Expression Tree accessors 18 /// </Summary> 19 public class NotEqualExpressionVisitor: ExpressionVisitor20 {21 public Expression Visit (BinaryExpression node) 22 {23 return VisitBinary (node ); 24} 25 26 protected override Expression VisitBinary (BinaryExpression node) 27 {28 return node. nodeType = ExpressionType. 29th? Expression. MakeBinary (ExpressionType. NotEqual, node. Left, node. Right) // obtain a new Expression: use! = Replace = 30: base. VisitBinary (node); 31} 32} 33}

 

 

Http://www.cnblogs.com/liqingwen/p/5868688.html.

 

Previewed version, updated on 09/16

 

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.