Finishing up some things in the expression tree, getting started is enough.
Let's start with constantexpression. It represents an expression with a constant value
We choose to build a console application
ConstantExpression _constexp = Expression.constant ("AAA",typeof(string));//a constant//Console.WriteLine ("AAA");Methodcallexpression _methodcallexp=expression.call (typeof(Console). GetMethod ("WriteLine",Newtype[]{typeof(string)}), _constexp); Expression<Action> Consolelambdaexp = expression.lambda<action>(_METHODCALLEXP); Consolelambdaexp.compile () (); Console.ReadLine ();
Below the methodcallexpression you may not know what is the matter, do not hurry I will be detailed below, this is equivalent
Console.WriteLine ("AAA"); Output a constant and look at the results
If you want to enter a value output yourself, then use ParameterExpression it represents a parameter expression, we just need to make a small change to the above code.
ParameterExpression _parameexp = Expression.parameter (typeof(string),"Myparameter"); Methodcallexpression _methodcallexpp= Expression.call (typeof(Console). GetMethod ("WriteLine",NewType[] {typeof(string)}), _parameexp); Expression<Action<string>> _consstringexp = expression.lambda<action<string>>(_methodcallexpp, _parameexp); _consstringexp.compile () ("hello!!");
Parameter parameexp is a variable of type string we let it output a hello!!
I feel a little bit, take it easy, it's still behind, now let's just say methodcallexpression it can call static methods and instance methods, and the code above is the static method of the call.
, let me start by calling the static method and then calling the instance method.
We build a static method that returns a string, passing in a value of type Object
Public Static string Consstr (object str) { string"aa"; Console.WriteLine (_STR); return _str;}
Take a look at how we call our own static methods.
ParameterExpression _paraobj = Expression.parameter (typeof(Object),"Objpara"); Methodcallexpression _mystatemethod= Expression.call (typeof(program). GetMethod ("Consstr",NewType[] {typeof(Object)}), _paraobj); Expression<Func<Object,string>> _meylambdastate = expression.lambda<func<Object,string>>(_mystatemethod, _paraobj);stringS_TR = _meylambdastate.compile () ("ni Hao"); Console.WriteLine ("return Value:"+ s_tr);
New type[] {typeof (Object)} is the parameter type in our method, the back of the paraobj is equivalent to the parameter value, if it is a multi-parameter in type[], and after the corresponding type and parameters on the line
Static methods you've got some idea, let's talk about calling instance methods
We write a non-static method
Public string CONSSTR2 (object str) { string"aa"; Console.WriteLine (_STR); return _str;}
Call when you just change the top of the code a little bit OK Expression.call provide us with the overload we want
Program _PG =NewProgram (); 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);stringS_TR2 = _meylambdastate.compile () ("You shi ni"); Console.WriteLine ("return Value:"+ S_TR2);
It's simple.
What are we talking about, maybe you guessed it? Unaryexpression unary operator expression and binaryexpression two-tuple operator expression
Let's look at a simple example of these two expressions, and then we'll do a complicated example.
Unaryexpression we make a 5--expression.
ConstantExpression _consnum = expression.constant (5typeof(int= Expression.decrement (_consnum); Expression<Func<int>> _unarylam = expression.lambda<func<int>> (_unaryplus); Console.WriteLine (_unarylam.compile ());
Binaryexpression We do an example of a+b
ParameterExpression _paraa = Expression.parameter (typeof(int),"a"); 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,Newparameterexpression[] {_paraa, _parab}); Console.WriteLine ("An expression:"+_mybinaryaddlamb); Console.WriteLine (_mybinaryaddlamb.compile () (3,6));
It's not hard,
Let's make an example of two expressions together (a+b) * (--C)
ParameterExpression _paraa = Expression.parameter (typeof(int),"a"); ParameterExpression _parab= Expression.parameter (typeof(int),"b"); Binaryexpression _binaadd= Expression.add (_paraa, _parab);//a+bparameterexpression _parac= Expression.parameter (typeof(int),"C"); Unaryexpression _PARADECR= Expression.decrement (_parac);//(a+b) * (--c)Binaryexpression _binamultiply =expression.multiply (_binaadd, _PARADECR); Expression<Func<int,int,int,int>> _mybinarylamb = expression.lambda<func<int,int,int,int>> (_binamultiply,Newparameterexpression[] {_paraa, _parab, _parac}); Console.WriteLine ("An expression:"+_mybinarylamb); Console.WriteLine (_mybinarylamb.compile () (3,6,5));
That's all for today.
Li Peng Source: http://www.cnblogs.com/li-peng/The author and the blog Park share, Welcome to reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Expression Tree Learning Finishing