. Net series: Expression expression Tree, Expression expression

Source: Internet
Author: User

. Net series: Expression expression Tree, Expression expression

Go to: http://www.cnblogs.com/li-peng/p/3154381.html

I sorted out some things about the expression tree. It's enough to get started.

First, start with ConstantExpression. It indicates an expression with a constant value.

Select a console application.

ConstantExpression _ constExp = Expression. Constant ("aaa", typeof (string); // a Constant
// Console. Writeline ("aaa ");
MethodCallExpression _ methodCallexp = Expression. Call (typeof (Console). GetMethod ("WriteLine", new Type [] {typeof (string)}), _ constExp );
Expression <Action> consoleLambdaExp = Expression. Lambda <Action> (_ methodCallexp );
ConsoleLambdaExp. Compile ()();

Console. ReadLine ();
You may not know what the following MethodCallExpression is. Don't worry. I will explain it in detail below, which is equivalent

Console. WriteLine ("aaa"); outputs a constant. Let's take a look at the result.

 

If you want to input a value for output, you can use ParameterExpression to represent a parameter expression. You only need to make minor changes to the above Code.

ParameterExpression _ parameExp = Expression. Parameter (typeof (string), "MyParameter ");

MethodCallExpression _ methodCallexpP = Expression. Call (typeof (Console). GetMethod ("WriteLine", new Type [] {typeof (string)}), _ parameExp );
Expression <Action <string> _ consStringExp = Expression. Lambda <Action <string> (_ methodCallexpP, _ parameExp );
_ ConsStringExp. Compile () ("Hello !! ");
ParameExp is a string type variable. Let it output a Hello !!

 

It's a bit of fun. It's still fun. Now let's talk about MethodCallExpression, which can call static methods and instance methods. The above code is the static method called.

First, let me talk about calling static methods, and then call the instance methods.

We create a static method that returns a string and input a value of the object type.

Public static string ConsStr (object str)
{
String _ str = str + "aa ";
Console. WriteLine (_ str );
Return _ str;
}
Let's see how we call our own static method.

ParameterExpression _ paraObj = Expression. Parameter (typeof (object), "objPara ");
MethodCallExpression _ MyStateMethod = Expression. Call (typeof (Program). GetMethod ("ConsStr", new Type [] {typeof (object)}), _ paraObj );
Expression <Func <object, string> _ meyLambdaState = Expression. Lambda <Func <object, string> (_ MyStateMethod, _ paraObj );
String s_tr = _ meyLambdaState. Compile () ("ni Hao ");
Console. WriteLine ("Return Value:" + s_tr );


New Type [] {typeof (object)} is the parameter Type in our method. The following paraObj is equivalent to the parameter value. If it is multiple parameters, it is in Type []. and add the corresponding types and parameters.

You have some knowledge about static methods. The following describes how to call an instance method.

Let's write a non-static method.

Public string ConsStr2 (object str)
{
String _ str = str + "aa ";
Console. WriteLine (_ str );
Return _ str;
}
When calling, you only need to change the code above. OK Expression. Call provides us with the required reload.

Program _ pg = new Program ();
ParameterExpression _ paraObj2 = Expression. Parameter (typeof (object), "objPara ");
MethodCallExpression _ MyStateMethod2 = Expression. Call (Expression. Constant (_ pg), typeof (Program). GetMethod ("ConsStr2"), _ paraObj2 );
Expression <Func <object, string> _ meyLambdaState2 = Expression. Lambda <Func <object, string> (_ MyStateMethod2, _ paraObj2 );
String s_tr2 = _ meyLambdaState. Compile () ("you shi ni ");
Console. WriteLine ("Return Value:" + s_tr2 );


Simple.

What can we talk about next? Maybe you have guessed the UnaryExpression and BinaryExpression

Let's take a look at a simple example of these two expressions and then make a complicated example.

UnaryExpression: Let's make a 5-expression.

ConstantExpression _ consNum = Expression. Constant (5, typeof (int ));
UnaryExpression _ unaryPlus = Expression. Decrement (_ consNum );
Expression <Func <int> _ unaryLam = Expression. Lambda <Func <int> (_ unaryPlus );
Console. WriteLine (_ unaryLam. Compile ()());


BinaryExpression: An Example of a + B

ParameterExpression _ ParaA = Expression. Parameter (typeof (int), "");
ParameterExpression _ ParaB = Expression. Parameter (typeof (int), "B ");
BinaryExpression _ BinaAdd = Expression. Add (_ ParaA, _ ParaB );
Expression <Func <int, int, int> _ MyBinaryAddLamb = Expression. lambda <Func <int, int, int> (_ BinaAdd, new ParameterExpression [] {_ ParaA, _ ParaB });
Console. WriteLine ("expression:" + _ MyBinaryAddLamb );
Console. WriteLine (_ MyBinaryAddLamb. Compile () (3, 6 ));


Not hard,

Let's take two expressions together for example (a + B) * (-- c)

ParameterExpression _ ParaA = Expression. Parameter (typeof (int), "");
ParameterExpression _ ParaB = Expression. Parameter (typeof (int), "B ");
BinaryExpression _ BinaAdd = Expression. Add (_ ParaA, _ ParaB); // a + B

ParameterExpression _ paraC = Expression. Parameter (typeof (int), "c ");
UnaryExpression _ paraDecr = Expression. Decrement (_ paraC); // (a + B) * (-- c)
BinaryExpression _ binaMultiply = Expression. Multiply (_ BinaAdd, _ paraDecr );
Expression <Func <int, int> _ MyBinaryLamb = Expression. lambda <Func <int, int> (_ binaMultiply, new ParameterExpression [] {_ ParaA, _ ParaB, _ paraC });
Console. WriteLine ("expression:" + _ MyBinaryLamb );
Console. WriteLine (_ MyBinaryLamb. Compile () (3, 6, 5 ));
  

 

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.