[. Net Object-Oriented programming advanced] (6) Lamda expressions (2) Expression Tree Quick Start, lamda expressions

Source: Internet
Author: User

[. Net Object-Oriented programming advanced] (6) Lamda expressions (2) Expression Tree Quick Start, lamda expressions

[. Net Object-Oriented programming advanced] (6) Lamda expressions (2) Expression Tree Quick Start

This section introduces:

Recognize the Expression Tree. Learn how to use Lambda to create an Expression Tree and parse the Expression Tree.

The advantages of learning expressions in programming: for example, constructing dynamic queries and dynamically Constructing Expression trees to access unknown object attributes are much higher than reflection performance. We can say that expression trees are the essence of Lambda and must be mastered and used flexibly.

1. Expression Tree) 

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

Expression Tree is hard to understand for new contacts. In general, what is an expression tree is to use a tree-like Data Structure to cache expressions.

2. What can the expression tree do for us? 

As we have mentioned above, the expression tree is a data structure. Why should we define the expression as an Expression Tree and convert it into a data structure? What can we do? We need to clarify what he can bring to us before we can continue to look at it.

Speaking of his role, it is of course very useful. We need to use him in many scenarios,When an expression changes to the data structure of an Expression Tree, the expression becomes an object. You can edit and operate the object elements (Expression Code) in the Expression Tree.. Below are several common examples:

3. Create an Expression Tree 

Namespace: System. Linq. Expressions 

Example of creating an Expression Tree:

Expression<Func<int, int, bool>>  expression  =  (num,num2)=> num < num2;

This statement contains three parts:

Expression is an Expression tree of expression <T> type. It can be seen that Expression is not an executable code, but a Data Structure of an expression tree. 

4. Structure of Expression Tree 

Expression<Func<int, int, bool>>  expression  =  (num,num2)=> num < num2;

By monitoring the defined Expression Tree, we can see its compositionThe following describes several important attributes: 

  • Body:Obtain the subject of the expression. Example: (num <num2) the Body contains the Left and Right Attributes. In the preceding example, num and num2 are respectively used.
  • Parameters: Get the lambda expression Parameters.Example {num, num2}
  • NodeType: Get the ExpressionType of the node of the tree. Total 45Contains various possible types of all expression nodes.For example, return constants, such as return parameters, such as taking the small values of two values (<), for example, getting the values of two values (>), for example, adding values (+), and so on. Example: Lambda
  • Type: obtain a static Type of the expression.For example, Func <int, int, bool>.
  • ReturnType: The return type of the expression.Example: bool

There are other attributes such as Name and CanReduce, which are not very common.

5. parse Expression Tree 

We have learned about the structure of the Expression Tree, and it is quite easy to parse the expression. Next we will use an example to parse an Expression Tree:

// Expression Tree (Expression) expression <Func <int, int, bool> Expression = (x, y) => x! = 0 & x = y + 1; BinaryExpression exr = expression. body as BinaryExpression; IReadOnlyList <ParameterExpression> param = expression. parameters as IReadOnlyList <ParameterExpression>; BinaryExpression left = exr. left as BinaryExpression; BinaryExpression right = exr. right as BinaryExpression; ExpressionType exrType = exr. nodeType; ParameterExpression leftLeft = left. left as ParameterExpression; ConstantExpression leftRight = left. right as ConstantExpression; ExpressionType leftType = left. nodeType; ParameterExpression rightLeft = right. left as ParameterExpression; BinaryExpression rightRight = right. right as BinaryExpression; ExpressionType rightType = right. nodeType; ParameterExpression rightRightLeft = rightRight. left as ParameterExpression; ExpressionType rightRightType = rightRight. nodeType; ConstantExpression rightRightRight = rightRight. right as ConstantExpression; Console. writeLine (exr. toString ());

When parsing the expression tree, there are only the following elements:

A. BinaryExpression: an expression that contains binary Operators 

B. parameter expression named after ParameterExpression, which is generally a set of parameter sets IReadOnlyList <ParameterExpression> 

C. Expression of the constant value of ConstantExpression 

D. ExpressionType Node Type 

For the expression tree structure shown in the preceding example, I use the legend as follows:

For the above structure, we can follow the object element variables in the above Code to correspond to one of the following:

 

6 key points:

This section describes the role of the Expression Tree in. NET Object-Oriented programming, and the creation and resolution of the expression tree. I believe our friends have a better understanding of the structure of the Expression Tree.

Expression Tree is an object-based Expression. This is also the embodiment of. NET's idea that everything is an object.

In the next section, we will introduce how to dynamically create an Expression Tree Structure and illustrate the application of several expression trees in. NET programming.

========================================================== ========================================================== ====================

 Returned directory

<If it is helpful to you, please click here for recommendations. If yesDo not understand or make a mistakePoints, Please exchange more>

<If you have any difficulty reading this series of articles, please read. net Object-Oriented Programming basics first>

<Reprinted statement: technology requires a spirit of sharing. You are welcome to repost the article in this blog, but please note the copyright and URL>

. NET exchange group: 467189533

========================================================== ========================================================== ====================

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.