How to: Modify Expression Trees (Visual Basic)
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 an
Imports
statement to the file for theSystem.Linq.Expressions
namespace.Add the
AndAlsoModifier
class to your project.Public Class AndAlsoModifier Inherits ExpressionVisitor Public Function Modify(ByVal expr As Expression) As Expression Return Visit(expr) End Function Protected Overrides Function VisitBinary(ByVal b As BinaryExpression) As Expression If b.NodeType = ExpressionType.AndAlso Then Dim left = Me.Visit(b.Left) Dim right = Me.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) End If Return MyBase.VisitBinary(b) End Function End Class
This class inherits the ExpressionVisitor class and is specialized to modify expressions that represent conditional
AND
operations. It changes these operations from a conditionalAND
to a conditionalOR
. To do this, the class overrides the VisitBinary method of the base type, because conditionalAND
expressions are represented as binary expressions. In theVisitBinary
method, if the expression that is passed to it represents a conditionalAND
operation, the code constructs a new expression that contains the conditionalOR
operator instead of the conditionalAND
operator. If the expression that is passed toVisitBinary
does not represent a conditionalAND
operation, 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 an
Imports
statement to the file for theSystem.Linq.Expressions
namespace.Add code to the
Main
method in the Module1.vb file to create an expression tree and pass it to the method that will modify it.Dim expr As Expression(Of Func(Of String, Boolean)) = _ Function(name) name.Length > 10 AndAlso name.StartsWith("G") Console.WriteLine(expr) Dim modifier As New AndAlsoModifier() Dim modifiedExpr = modifier.Modify(CType(expr, Expression)) 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
AND
operation. It then creates an instance of theAndAlsoModifier
class and passes the expression to theModify
method of this class. Both the original and the modified expression trees are outputted to show the change.Compile and run the application.