Examining the Structure of an Expression using C# with Example



Examining the Structure of an Expression using C# with Example

Visitor 
Define a new visitor class by overriding some of the methods of ExpressionVisitor: 
class PrintingVisitor : ExpressionVisitor { 
protected override Expression VisitConstant(ConstantExpression node) { 
Console.WriteLine("Constant: {0}", node); 
return base.VisitConstant(node); 
} 
protected override Expression VisitParameter(ParameterExpression node) { 
Console.WriteLine("Parameter: {0}", node); 
return base.VisitParameter(node); 
} 
protected override Expression VisitBinary(BinaryExpression node) { 
Console.WriteLine("Binary with operator {0}", node.NodeType); 
return base.VisitBinary(node); 
} 
} 
Call Visit to use this visitor on an existing expression: 
Expression> isBig = a => a > 1000000; 
var visitor = new PrintingVisitor(); 
visitor.Visit(isBig); 

0 Comment's

Comment Form

Submit Comment