How to modify expression trees (C#)
This topic shows you how to modify an expression tree. Expression trees are immutable, which means that they cannot be modified directly. To change an expression tree, you must create a copy of an existing expression tree and when you create the copy, make the required changes. You can use the ExpressionVisitor class to traverse an existing expression tree and to copy each node that it visits.
To modify an expression tree
Create a new Console Application project.
Add a
usingdirective to the file for theSystem.Linq.Expressionsnamespace.Add the
AndAlsoModifierclass to your project.public class AndAlsoModifier : ExpressionVisitor { public Expression Modify(Expression expression) { return Visit(expression); } protected override Expression VisitBinary(BinaryExpression b) { if (b.NodeType == ExpressionType.AndAlso) { Expression left = this.Visit(b.Left); Expression right = this.Visit(b.Right); // Make this binary expression an OrElse operation instead of an AndAlso operation. return Expression.MakeBinary(ExpressionType.OrElse, left, right, b.IsLiftedToNull, b.Method); } return base.VisitBinary(b); } }This class inherits the ExpressionVisitor class and is specialized to modify expressions that represent conditional
ANDoperations. It changes these operations from a conditionalANDto a conditionalOR. To do this, the class overrides the VisitBinary method of the base type, because conditionalANDexpressions are represented as binary expressions. In theVisitBinarymethod, if the expression that is passed to it represents a conditionalANDoperation, the code constructs a new expression that contains the conditionalORoperator instead of the conditionalANDoperator. If the expression that is passed toVisitBinarydoes not represent a conditionalANDoperation, the method defers to the base class implementation. The base class methods construct nodes that are like the expression trees that are passed in, but the nodes have their sub trees replaced with the expression trees that are produced recursively by the visitor.Add a
usingdirective to the file for theSystem.Linq.Expressionsnamespace.Add code to the
Mainmethod in the Program.cs file to create an expression tree and pass it to the method that will modify it.Expression<Func<string, bool>> expr = name => name.Length > 10 && name.StartsWith("G"); Console.WriteLine(expr); AndAlsoModifier treeModifier = new AndAlsoModifier(); Expression modifiedExpr = treeModifier.Modify((Expression) expr); Console.WriteLine(modifiedExpr); /* This code produces the following output: name => ((name.Length > 10) && name.StartsWith("G")) name => ((name.Length > 10) || name.StartsWith("G")) */The code creates an expression that contains a conditional
ANDoperation. It then creates an instance of theAndAlsoModifierclass and passes the expression to theModifymethod of this class. Both the original and the modified expression trees are outputted to show the change.Compile and run the application.
See also
Maklum balas
Kirim dan lihat maklum balas untuk