C # advanced programming (9) -- Expression Tree

Source: Internet
Author: User

The Expression Tree is designed based on the idea of "code as data". It expresses the code into a tree-like data structure, each node in the tree structure is an expression (this expression is a broad concept, not the expression syntax in programming languages). Therefore, it is called an Expression Tree.
The essence of the Expression Tree is to organize the code in the data segment rather than the code segment, which is very important for changing the code at runtime.
System. linq. the Expressions namespace contains many classes to express different Expressions. These classes inherit from the abstract Expression base class. Expression contains a wide range of static methods for creating various Expression classes.
I. Build Expression Tree by programming
The following code constructs an Expression Tree programmatically
[Csharp]
Expression firstArg = Expression. Constant (2 );
Expression secondArg = Expression. Constant (3 );
Expression add = Expression. Add (firstArg, secondArg );
Console. WriteLine (add );

Expression Tree created by the above Code

2. Expression Tree and proxy (compile Expression Tree as proxy)
The key to converting the Expression tree into a proxy is the Expression <TDelegate> class, which inherits the relationship.

You can use the Expression. Lambda Method to create an Expression <TDelegate> object. The Expression <TDelegate> object contains the Compile method, which is used to Compile the Expression into executable code and generate a proxy object that represents the lambda Expression,
The following code indicates the conversion process:
[Csharp]
Expression firstArg = Expression. Constant (2 );
Expression secondArg = Expression. Constant (3 );
Expression add = Expression. Add (firstArg, secondArg );
Func <int> compiled = Expression. Lambda <Func <int> (add). Compile ();
Console. WriteLine (compiled ());

3. Expression Tree and Lambda expressions (converting Lambda expressions to Expression Tree)
You can use a lambda Expression to construct an Expression <TDelegate> object:
[Csharp]
Expression <Func <string, string, bool> expression =
(X, y) => x. StartsWith (y );
Var compiled = expression. Compile ();
Console. WriteLine (compiled ("First", "Second "));
Console. WriteLine (compiled ("First", "Fir "));

The following code is equivalent to this, but the expression tree is constructed programmatically:
[Csharp]
MethodInfo method = typeof (string). GetMethod
("StartsWith", new [] {typeof (string )});
Var target = Expression. Parameter (typeof (string), "x ");
Var methodArg = Expression. Parameter (typeof (string), "y ");
Expression [] methodArgs = new [] {methodArg };
Expression call = Expression. Call (target, method, methodArgs );
Var lambdaParameters = new [] {target, methodArg };
Var lambda = Expression. Lambda <Func <string, string, bool>
(Call, lambdaParameters );
Var compiled = lambda. Compile ();
Console. WriteLine (compiled ("First", "Second "));
Console. WriteLine (compiled ("First", "Fir "));

Currently, not all lambda expressions can be converted to an Expression Tree. Only a single lambda expression can be converted to an Expression Tree, and the expression cannot contain values.

Iv. Expression Tree and LINQ
The Expression Tree is mainly used in LINQ to SQL. The objective of LINQ to SQL is to convert a LINQ request to an SQL statement (common text ), however, during the conversion process, we do not want to lose the type check during compilation, therefore, the design idea of LINQ to SQL is to convert a LINQ Query into an Expression Tree (the lambda expression used in the query is converted to an Expression Tree according to a certain algorithm ), then convert the expression tree to the SQL statement to be executed.

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.