The shallow solution of the Expression Tree in C,

Source: Internet
Author: User

The shallow solution of the Expression Tree in C,

The expression tree can be said to be one of the core of Linq. Why is it one of the core of Linq? Because the expression tree makes c # no longer only be compiled into IL, we can use c # to generate an Expression Tree and use the result as an intermediate format, convert it to the local language on the target platform. For example, SQL. The commonly used Linq to SQL statements generate SQL statements in this way.

The Expression Tree was introduced after. NET 3.5. It is a powerful and flexible tool (for example, it is used to construct dynamic queries in LINQ ).

Let's take a look at the API interfaces of the Expression class:

Namespace System. Linq. Expressions {/// Abstract: // use the expression directory tree to represent a strong lambda expression as a data structure. This class cannot be inherited. //// Type parameter: // TDelegate: // System. Linq. Expressions. expression'1 indicates the type of the delegate. Public sealed class Expression <TDelegate>: LambdaExpression {// Abstract: // compile the lambda Expression described in the Expression tree into executable code and generate a delegate that represents the lambda Expression. //// Return result: // A TDelegate-type delegate that represents the compiled lambda Expression described by System. Linq. Expressions. expression'1. Public TDelegate Compile (); // Abstract: // generate a delegate representing the lambda expression. //// Parameter: // debugInfoGenerator: // The debugging information generator used by the compiler to mark sequence points and annotate local variables. //// Return result: // contains the delegate of the compiled version of lambda. Public TDelegate Compile (DebugInfoGenerator debugInfoGenerator); // Abstract: // create a new expression similar to this expression, but use the provided sub-level. If all child levels are the same, this expression is returned. //// Parameter: // body: // The System. Linq. Expressions. LambdaExpression. Body attribute of the result. //// Parameters: // The System. Linq. Expressions. LambdaExpression. Parameters attribute of the result. //// Return result: // This expression (if no child level is changed) or contains an updated child level expression. Public Expression <TDelegate> Update (Expression body, IEnumerable <ParameterExpression> parameters); protected internal override Expression Accept (ExpressionVisitor visitor );}}

The syntax of the Expression Tree is as follows:

Expression<Func<type,returnType>> = (param) => lamdaexpresion;

For example:

Expression<Func<int, int, int>> expr = (x, y) => x+y;

Run the above Code and view the Expression Tree in the VS debugging mode:

The Expression Tree consists of the following four parts:

1. Body

2. Parameters

3. NodeType Node Type

4. Lambda expression type

In the above Code, the subject is x + y, the parameter is (x, y), NodeType is a Lambda expression, and the return value is int.

The subject part can be an expression but cannot contain statements. For example, if I define a delegate, Lambda expressions can be written like this.

Func<int, int, int> func = (x, y) => x + y;

You can also write as follows:

Func<int, int, int> func = (x, y) => { return x + y; };

However, in the Expression Tree, only the first method can be used. If the second method is used to compile and report an error, the lambda expression with the statement body cannot be converted to the expression tree.

In addition to the above writing method, the expression tree can also be written as follows:

 

ParameterExpression pex1 = Expression. parameter (typeof (int), "x"); // The first Parameter ParameterExpression pex2 = Expression. parameter (typeof (int), "y"); // The second Parameter BinaryExpression bexp = Expression. add (pex1, pex2); // Add var lambdaExp = Expression. lambda <Func <int, int, int> (bexp, new ParameterExpression [] {pex1, pex2 });

 

In VS debugging mode, we can see that the expression tree generated by the two writing methods is the same.

Compile the expression tree into a delegate

LambdaExpression is a type derived from Expression. The generic Expression <TDelegate> is derived from LambdaExpression. The generic parameter TDelegate must be of the delegate type.

LambdaExpression has a Compile method that can create a delegate of the appropriate type. The Compile method of Expression <TDelegate> returns the delegate of the TDelegate type. Let's take a look at the following example:

Expression <Func <int, int, int> expr = (x, y) => x + y; ParameterExpression pex1 = Expression. parameter (typeof (int), "x"); // The first Parameter ParameterExpression pex2 = Expression. parameter (typeof (int), "y"); // The second Parameter BinaryExpression bexp = Expression. add (pex1, pex2); // subject, addition // use Expression. lambda method to create an ExpressionExpression <Func <int, int, int> lambdaExp = Expression. lambda <Func <int, int, int> (bexp, new ParameterExpression [] {pex1, pex2}); Func <int, int, int> tDelegate = lambdaExp. compile (); // Compile it into the delegate Console. writeLine (tDelegate (1, 3); Console. read ();

Run the above Code and the result is: 4. We have written a lot of code. In essence, we use the expression tree to calculate the result of 1 + 3.

 

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.