Extract from Expression Tree (2

Source: Internet
Author: User

Some relatively simple expressions, such as constantexpression, parameterexpression, unaryexpression, binaryexpression, newexpression, newarrayexpression, and typebinaryexpression, are learned in this article.

Content
Conditionalexpression
Invocationexpression: an expression that applies a delegate or Lambda expression to the parameter expression list.
Lambdaexpression indicates a Lambda expression.
Listintiexpression indicates a constructor call that contains the set's initial values.
Memberexpression: an expression that represents an access field or attribute.
Introduction to memberinitexpression
Methodcallexpression describes how to call a method expression.

Introduction to conditionalexpression
This class encapsulates expressions containing conditional operators. It creates the conditionalexpression using the condition factory method, and its nodetype is conditional.

Attribute
Iffalse: gets the expression to be executed when the test result is false.
Iftrue: gets the expression to be executed when the test result is true.
Test

Factory method Condition

Example
The following code example demonstrates how to create an expression to represent a conditional statement. If the calculation result of the first parameter is true, the second parameter is executed; otherwise, the third parameter is executed.
Running result:

Introduction to invocationexpression
This class encapsulates the expressions that apply the delegate or Lambda expression to the parameter expression list. The invocationexpression is created using the invoke factory method, and its nodetype is invoke.

Attribute
Arguments obtains the parameters delegated to its application.
Expression gets the delegate or Lambda expression to be applied

Example
The following example shows how to use the invoke (expression, expression []) method to create an invocationexpression, which indicates that the lambda expression is called using the specified parameter.

 

Running result:

Introduction to lambdaexpression
This class encapsulates a Lambda expression. The lambdaexpression type represents a Lambda expression in the form of an expression directory tree. The expression <tdelegate> type is derived from lambdaexpression. It can capture the type of a Lambda expression more explicitly or represent a Lambda expression. During runtime, the expression directory tree node of the lambda expression always belongs to the type expression <tdelegate>.

 

The value of the nodetype attribute of lambdaexpression is lambda.

 

Use the lambda factory method to create a lambdaexpression object.

Member
Body obtains the body of a Lambda expression.
Parameters
Compile () methodIt is mainly used to generate a delegate that represents a Lambda expression.

Lambda factory Method
Create an Expression Tree that represents a Lambda expression, and reload

 

Example
The following example demonstrates the process of creating a Lambda expression (a, B) => (A * B) + 2 ).

Running result

 

 

 

Introduction to listinitexpression
This class encapsulates a constructor call that contains the set's initial values. It mainly uses the listinit factory method to create listinitexpression. Its nodetype attribute value is listinit.

Main attributes
InitializersReturns the <elementinit> type of readonlycollection.
NewexpressionObtain the call expression containing the constructor of the set type and return the newexpression type.

Factory method

Example
In the following example, an elementinit is created to initialize an element in the dictionary set.
 
Result
Introduction to memberexpression
This class indicates the access field or attribute. memberexpression is created by using the field, property, or propertyorfield factory method, and its nodetype attribute value is memberaccess.

Attribute
Expression obtains the contained objects of a field or attribute.

Member obtains the field or attribute to be accessed.

Factory method
Field (expression, fieldinfo) creates a memberexpression that represents the access field.
Field (expression, string) creates a memberexpression that indicates access to the field given the field name.
Property (expression, methodinfo) uses the attribute accessors method to create a memberexpression that represents the access attribute.
Property (expression, propertyinfo) creates a memberexpression that represents the access attribute
Property (expression, string) creates a memberexpression that indicates access to this attribute given the attribute name.
Propertyorfield (expression, string) creates a memberexpression that indicates access to this attribute or field when the attribute or field name is specified.

Example
The following example creates a memberexpression to obtain the value of a field member.

Running result:

Introduction to memberinitexpression
This class indicates calling the constructor and initializing one or more members of the new object. The memberinitexpression is created mainly through the memberinit factory method, and its nodetype value is memberinit.

Main attributes
BindingsReturns the readonlycollection <memberbinding> type.
NewexpressionReturns the newexpression type.

Factory method
Memberinit (newexpression, ienumerable <memberbinding>) creates a memberinitexpression.
Memberinit (newexpression, memberbinding []) creates a memberinitexpression.

Example

 


Result

 

 
Introduction to methodcallexpression
This class indicates that a method is called. You can use the call and arrayindex factory methods to create a methodcallexpression. The value of nodetype is call.

Attribute
Arguments: Obtain the public readonlycollection parameter of the called method <expression> arguments {Get ;}
Method gets the called method public methodinfo method {Get ;}
Object Receiving Method public expression object {Get ;}

Factory method
Call (expression, methodinfo) creates a methodcallexpression that indicates that a method without parameters is called.
Call (methodinfo, expression []) to create a methodcallexpression that calls the static method
Call (expression, methodinfo, ienumerable <expression>) to create a methodcallexpression that calls methods with parameters.
Call (expression, methodinfo, expression []) creates a methodcallexpression that calls methods with parameters.
Call (expression, String, type [], expression []) creates a methodcallexpression that calls the instance method by calling the appropriate factory Method
Call (type, String, type [], expression []) creates a methodcallexpression that calls the static method by calling the appropriate factory Method
Arrayindex (expression, ienumerable <expression>) creates a methodcallexpression that applies the array index operator to an array with a rank greater than 1.
Arrayindex (expression, expression) creates a binaryexpression that applies the array index operator to an array with the rank of 1.
Arrayindex (expression, expression []) creates a methodcallexpression that applies the array index operator to arrays whose rank is greater than 1.

Example
The following example shows how to create an expression that calls a static method using a parameter.

Running result:

 

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.