Class CooperativeExpressionVisitor
Cooperative expression visitor that dispatches expression tree node visits to other visitors based on metadata attributes applied to reflection objects.
Inheritance
Namespace: System.Linq.CompilerServices
Assembly: Nuqleon.Linq.CompilerServices.dll
Syntax
public class CooperativeExpressionVisitor : ExpressionVisitor
Examples
Consider the following method definition:
[Visitor(typeof(MyVisitor))]
static int Foo(int x)
{
throw new NotImplementedException();
}
In here, the Foo method is annotated with the VisitorAttribute, specifying the type of the visitor that will process nodes using this method, e.g. in a MethodCallExpression. This type implements IExpressionVisitor:
class MyVisitor : IRecursiveExpressionVisitor
{
public bool TryVisit(Expression expression, Func<Expression, Expression> visit, out Expression result)
{
// No-op rewrite for illustration purposes
var call = (MethodCallExpression)expression;
var arg = visit(call.Arguments[0]);
result = call.Update(null, new[] { arg });
return true;
}
}
Using the CooperativeExpressionVisitor, we can visit a tree that calls Foo:
var f = (Expression<Func<int, int>>)(x => Foo(x + 1));
var g = new CooperativeExpressionVisitor().Visit(f);
Constructors
CooperativeExpressionVisitor()
Declaration
public CooperativeExpressionVisitor()
Methods
VisitBinary(BinaryExpression)
Visits a BinaryExpression. If the binary expression is using operator overloading, the MethodInfo of the operator implementation is considered for cooperative rewriting.
Declaration
protected override Expression VisitBinary(BinaryExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.BinaryExpression | node | The expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The modified expression, if it or any subexpression was modified; otherwise, returns the original expression. |
VisitIndex(IndexExpression)
Visits an IndexExpression. The PropertyInfo of the indexing operation is considered for cooperative rewriting.
Declaration
protected override Expression VisitIndex(IndexExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.IndexExpression | node | The expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The modified expression, if it or any subexpression was modified; otherwise, returns the original expression. |
VisitMember(MemberExpression)
Visits a MemberExpression. The MemberInfo of the member lookup is considered for cooperative rewriting.
Declaration
protected override Expression VisitMember(MemberExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.MemberExpression | node | The expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The modified expression, if it or any subexpression was modified; otherwise, returns the original expression. |
VisitMethodCall(MethodCallExpression)
Visits a MethodCallExpression. The MethodInfo of the method call is considered for cooperative rewriting.
Declaration
protected override Expression VisitMethodCall(MethodCallExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.MethodCallExpression | node | The expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The modified expression, if it or any subexpression was modified; otherwise, returns the original expression. |
VisitNew(NewExpression)
Visits a NewExpression. The ConstructorInfo of the constructor call is considered for cooperative rewriting.
Declaration
protected override Expression VisitNew(NewExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.NewExpression | node | The expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The modified expression, if it or any subexpression was modified; otherwise, returns the original expression. |
VisitUnary(UnaryExpression)
Visits a UnaryExpression. If the unary expression is using operator overloading, the MethodInfo of the operator implementation is considered for cooperative rewriting.
Declaration
protected override Expression VisitUnary(UnaryExpression node)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.UnaryExpression | node | The expression to visit. |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression | The modified expression, if it or any subexpression was modified; otherwise, returns the original expression. |