[. NET Object-oriented Programming advanced ] (6) Lamda expression ( two ) Quick start of expression tree
This section is guided by:
Recognize expression trees , learn to use Lambda to create an expression tree, and parse an expression tree.
The advantages of learning expressions in program design are: for example, constructing dynamic query and dynamically constructing expression tree to complete unknown object property access, which is much higher than reflection performance. We can say that the expression tree is the essence of LAmbda, we must be proficient and flexible to use.
1. About an expression tree
expression trees represent code in a tree-like structure, where each node is an expression , such as a method call and a two-dollar operation such as x < y .
Expression trees are more difficult to understand for people who are just touching. So what is an expression tree, popular point, is to use a tree-shaped data structure to cache expressions.
2. What can the expression tree do for us?
Above we say that the expression tree is a data structure, then why the expression is defined as an expression tree, and turn into a data structure, can help us do something? It is clear what he can bring to us before we continue to see.
Speaking of his role, of course, is very useful, we have to use him in many scenarios, when the expression becomes a data structure of an expression tree, the expression becomes an object, you can edit and operate the object element (the code of the expression) in the expression tree . Here are a few common examples:
- Can modify executable code dynamically by modifying expression tree
- To execute a LINQ query in a different database
- Create a dynamic query.
- Complete similar reflection access to unknown object properties, generating a delegate by dynamically constructing an expression tree.
3. Creation of an expression tree
namespaces: System.Linq.Expressions
To create an expression tree example:
expression<func<intintbool>> Expression = (num,num2) = > num < num2;
This statement consists of three parts:
- A declaration : func<int, int, int> function
- An equal sign: =
- A lambda expression : (num,num2)-num < num2;
Expression is a tree of expressions of type expression<t>, and it can be seen that he is not an executable code, but rather a data structure of an expression tree.
4. composition of the expression tree structure
expression<func<intintbool>> Expression = (num, NUM2)-num < num2;
by monitoring Our well-defined expression tree, we can see its composition
Here's a description of several important attributes:
- Body: Gets the body of an expression . Example: (num < num2) where theBody contains the properties LEFT and Right In the example above, num and num2 are respectively
- Parameters: gets The parameters of the lambda expression. Example {num,num2}
- NodeType: Gets the expressiontype of the node of the tree . A different value that contains the various possible types of all expression nodes. For example, returns a constant, such as a return parameter, such as a small value of two values (<), such as a large value of two values (>), such as adding a value (+) , and so on. Example: Lambda
- Type: gets a static type of the expression . Example: func<int, int, bool>.
- ReturnType: This is the return type of an expression Example: BOOL
There are some other properties name,canreduce , and so on, which are not very common.
5. Parse An expression tree
We understand the composition of the expression tree structure, and the parsing of the expression becomes quite easy, so let's go through an example of parsing an expression tree directly:
//expression tree (expressions)expression<func<int,int,BOOL>> expression = (x, y) = x!=0&& x==y+1; Binaryexpression EXR= expression. Body asbinaryexpression;ireadonlylist<ParameterExpression> param = expression. Parameters asIreadonlylist<parameterexpression>; Binaryexpression Left= Exr. Left asbinaryexpression; Binaryexpression Right= Exr. Right asbinaryexpression; Expressiontype Exrtype=EXR. NodeType; ParameterExpression Leftleft= left. Left asparameterexpression; ConstantExpression LeftRight= left. Right asconstantexpression; Expressiontype LeftType=Left . NodeType; ParameterExpression Rightleft= right. Left asparameterexpression; Binaryexpression RightRight= right. Right asbinaryexpression; Expressiontype RightType=Right . NodeType; ParameterExpression Rightrightleft= Rightright.left asparameterexpression; Expressiontype Rightrighttype=Rightright.nodetype; ConstantExpression Rightrightright= Rightright.right asconstantexpression; Console.WriteLine (EXR. ToString ());
The parsing of the expression tree contains only the following elements:
a.binaryexpression An expression containing a two-tuple operator
b.parameterexpression a named parameter expression , typically a set of parameter sets ireadonlylist<parameterexpression>
c.constantexpression expressions for constant values
d.expressiontype Node Type
For the expression tree structure of the example above, I illustrate the following in the way of the legend:
For the above structure, we can press the OBJECT element variable in the above code with one pair as follows:
6 key points:
In this section, we describe the role of expression trees in . NET Object-oriented programming, the creation and parsing of expression trees, and believe that the small partners understand the structure of the expression tree.
An expression tree is a way to describe an expression by using an object. That's what . NET is all about the idea of the object.
In the next section we will describe the dynamic creation of an expression tree structure, which will give a list of the application of several expression trees in . NET programming.
==============================================================================================
Back to Catalog
< If it is helpful to you, please remember to click on the recommendation Oh, if there is There is no understanding or error , please exchange >
< for those who have difficulty reading this series of articles, please read the basics of. NET object-oriented programmingfirst >
< REPRINT statement: Technology needs to share the spirit, welcome to reprint the article in this blog, but please specify copyright and url>
. NET Technology Exchange Group: 467189533
==============================================================================================
[. NET object-oriented programming advanced] (5) Lamda expression (b) Quick start of an expression tree