C # Advanced Programming 65-day----expression tree

Source: Internet
Author: User

An expression tree

I've never heard of an expression tree before.,I've heard only expressions.,and I've heard it.LambdaAn expression,First Introduce. NETThe core concept of the expression tree in:speak code as data,He represents some code as an object tree,each node in the tree itself is an expression,different expression types represent the ability to perform different actions in code:two USD operation,unary Operation,method Invocation, and so on.

The System.Linq.Expression namespace contains the individual classes that represent the expression . all expression classes are from the Expression

The

class derives ,expresssion Span style= "Font-family:times New Roman", mainly includes some static methods , These methods are used to generate an instance of another expression class . Expression :

Type Property : represents the types of expression evaluation results . like , a , An expression is to get a string of Length Properties , then the expression's Type property should be int type .

NodeType Property : represents the kind of expression . This type is expressed as Expression Type a member of the American drama : Lessthan,invoke,

Multiply,memberaccess ( There seems to be more than one ).


Case :

Expression Firstarg = expression.constant (2);

Expression Secondarg = expression.constant (4);

Expression add = Expression.add (Firstarg, Secondarg);

Console.WriteLine (add);

the output is : <2 + 4>

Console.readkey ();

Analysis : The expression above produces the following expression :




I suddenly say that, you must think I am bluffing you , i cheat , like me a gentleman will not deceive you . The "leaf" expression in the expression is first created in the code: the expression is built from the bottom up . this is achieved by the fact that the expression is not easy to change .

Compiling an expression tree into a delegate

LambdaExpression is a type derived from Expression . generic class expression<tdelegate> is from lamdaexpression derived from , which reflection parameters tdelegate must be a delegate type .

LambdaExpression has a comlile method to create a delegate of the appropriate type . and expression<tdelegate> of the Compile method returns tdelegate type of Delegate . cases are as follows :

Expression Firstarg = expression.constant (2);

Expression Secondarg = expression.constant (4);

Expression add = Expression.add (Firstarg, Secondarg);

expression<func<int>> Func = expression.lambda<func<int>> (add);

Func<int> compiled = Func.compile ();

Console.WriteLine (compiled);

Console.readkey ();

Analysis : We use the expression.lambda<tdelegate> (expression expression) method to create Expression<tdelegate> Type Object , and then calls its Complie method to get the delegate instance compiled by the expression tree .

Convert an c#lambda expression to an expression tree

We know that Lambda expressions can be displayed or implicitly converted to the appropriate delegate instance . but , The compiler can also easily Lambda expressions are built into an expression tree :

Convert a Lambda expression to an expression tree

Expression <Func<int>> return 5= () =>5;

But,not all of them.Lambdaexpression can be converted to an expression tree,There are some limitations:a block with a statement cannot beLambdaConvert to an expression tree----only the right expression is evaluated.Lambdacan only.The expression cannot contain an assignment operation,because the expression tree does not represent this type of operation.There are some other rare limitations,In summary,If there is a conversion problem,will be found at compile time.

expression tree at the core of LINQ

An expression tree can be said to beLINQone of the core,Why isLINQone of the core??because an expression tree makesC #is no longer just compiled intoIL,we can passC #generate an expression tree,Use the result as an intermediate format,when you convert it to a native language on the target platform.likeSQL.we used toLINQ to SQLThat 's how it's generated..

Shows the different paths for LINQ to Objects and LINQ to SQL

Use of expression trees :

Through the various node types in the derived class of expression , We can construct an expression tree ; you can then convert the expression tree to the corresponding instance of the delegate type , the last code to execute the delegate instance , but we're not going to go around this big Bend. Execute the code for the delegate instance .

An expression tree is primarilyLINQ to SQLused in,we need toLINQ to SQLQuery Expressions(returnqueryabletype)Convert to an expression tree.The reason why you need to convert is becauseLINQ to SQlthe query expression is notC #code that executes the, LINQ to SQLThe query expression is converted to anSQL,send over the network,finally executed in the database server.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # Advanced Programming 65-day----expression tree

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.