Go to: http://www.cnblogs.com/li-peng/p/3154381.html
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", New Type[]{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", new type[] {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 _str = str + "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", 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 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 _str = str + "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 = 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);
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 (5, typeof (int));
Unaryexpression _unaryplus = 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, new parameterexpression[] {_paraa, _parab});
Console.WriteLine ("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+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, int, int>> _mybinarylamb = expression.lambda<func<int, int, int, int> > (_binamultiply, New parameterexpression[] {_paraa, _parab, _parac});
Console.WriteLine ("Expression:" + _mybinarylamb);
Console.WriteLine (_mybinarylamb.compile () (3, 6, 5));
. NET series: expression expressions tree