Just because someone stumbles loses their ' t way,it doesn ' re mean they. Sometimes we need a little help. The occasional loss of a person's direction does not mean that they will never be lost. Sometimes we just need someone to take a hand.
Program.cs
Class program
{
static void Main (string[] args)
{
baseexpression.test ();
Baseexpressionvisitortest.test ();
Console.read ();
}
BaseExpression.cs Code:
<summary>///Basic expression tree///</summary> public static void Test () {
ParameterExpression a = Expression.parameter (typeof (int), "a");
ParameterExpression B = Expression.parameter (typeof (int), "B");
Binaryexpression left = Expression.multiply (A, b);
ConstantExpression right = Expression.constant (2, typeof (int));
Binaryexpression BODY = Expression.add (left, right);
LambdaExpression lambda = expression.lambda<func<int, int, int>> (body, A, b); Console.WriteLine (lambda expression +lambda).
ToString ());
Compiles the lambda expression described by an expression tree into executable code and generates a delegate that represents the lambda expression.
Func<int, int, int> TTT = Lambda.compile () as func<int, int, int>; Console.WriteLine ("DynamicInvoke is the result:" + lambda.compile ().
DynamicInvoke (3, 4));
Console.WriteLine (TTT (3, 4)); //--------------------------------------------------------------------------------------------expression<func<int, int, int>> NEWLAMBDA = expression.lambd
A<func<int, int, int>> (body, A, b); Console.WriteLine ("Newlambda expression:" + Newlambda.)
ToString ());
Console.WriteLine (Newlambda.compile () (3, 4));
Func<int, int, int> MYLAMBDA = Newlambda.compile (); --------------------------------------------------------------------------------------------expression<f
Unc<int, int, int>> L2 = (x, y) => x + y + 3 + 4; Console.WriteLine ("Third expression tree Example:" +l2.)
ToString ()); Func<int, int> z1 = Z => z + 3;
This is an expression, not an expression tree. Console.WriteLine ("This is an example of an expression:" + z1.)
ToString ()); }
BaseExpressionVisitor.cs Code, BaseExpressionVisitorTest.cs code
public class Baseexpressionvisitor:expressionvisitor {//First we have a class that inherits from ExpressionVisitor, which has a modify method of our own, and then I We can call the visit method directly. We mentioned above that the//visit method is actually a portal that calls other visit methods based on the type of expression, and all we have to do is to find the corresponding method rewrite. But here's a bunch of//visit methods, which ones we're going to cover. It depends on our expression type, and in our where extension method, the expression tree we pass in is made up of/
The Expression.call method is constructed, and it returns the Methodcallexpression so our first step is to cover visitmethodcall.
Public Expression Modify (Expression exp) {return Visit (exp); //Translate expression tree Two-//expressionvisitor class is a helper class that is provided to our expression tree parsing, we simply define a class to inherit ExpressionVisitor, implement a Resolveexpressi On entry method,//Rewrite visitbinary, Visitconstant, Visitmember method, code as follows: protected override Expression Visitbinary (Binar Yexpression node) {if (node). NodeType = = Expressiontype.add) {var left = this. Visit (node.
left); var right = this. Visit (node.
right);
Return to Expression.subtract (left, right); Return to base.
Visitbinary (node); } public class Baseexpressionvisitortest {public static void Test () {Expressi
On<func<int, int, int>> expression = (A, b) => A + b * 2; Expression newexp = new Baseexpressionvisitor ().
Modify (expression);
Console.WriteLine ("Before Modification:"); Console.WriteLine (expression.
ToString ());
Console.WriteLine ("Modified:");
Console.WriteLine (Newexp.tostring ()); }
}
Project Run Results: