[C #] I just want to briefly describe the Expression Tree,

Source: Internet
Author: User

[C #] I just want to briefly describe the Expression Tree,
I just want to briefly describe the Expression Tree-Expression Trees

 

Directory
  • Introduction
  • Lambda expressions create Expression Tree
  • API creation Expression Tree
  • Parse Expression Tree
  • Permanent Expression Tree
  • Compile Expression Tree
  • Execution Expression Tree
  • Modify Expression Tree
  • Debugging

 

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 interoperability between dynamic languages and. NET frameworks.

 

1. Create an expression tree using 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;

 

Ii. API creation Expression Tree

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); // 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. Modify the Expression Tree

This class inherits the ExpressionVisitor class and indirectly calls the VisitBinary method through the Visit method! = Replace with =. 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}

1 ParameterExpression pExpression1 = Expression. Parameter (typeof (string); 2 ParameterExpression pExpression2 = Expression. Parameter (typeof (string), "msg ");

Figure 8-1

Figure 8-2

DebugView shows that if a parameter has no name, an automatically generated name is assigned to it.

 

1             const int num1 = 250;2             const float num2 = 250;3 4             ConstantExpression cExpression1 = Expression.Constant(num1);5             ConstantExpression cExpression2 = Expression.Constant(num2);

Figure 8-3

Figure 8-4

According to DebugView, float has more suffix F than int.

 

1             Expression lambda1 = Expression.Lambda<Func<int>>(Expression.Constant(250));2             Expression lambda2 = Expression.Lambda<Func<int>>(Expression.Constant(250), "CustomName", null);

Figure 8-5

Figure 8-6

Observe DebugView. If the lambda expression has no name, an automatically generated name is assigned to it.

 

 

 

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

[Reference] Microsoft official documentation

 

 

Related Article

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.