Some stupid task about Expression trees.
Write down a simplest expression tree visitor (inherited from System.Linq.Expressions.ExpressionVisitor) that will have a static method:
Write down a simplest expression tree visitor (inherited from System.Linq.Expressions.ExpressionVisitor) that will have a static method:
public static IEnumerable<Expression>
All(Expression
node)
This method should return lazy enumeration of all expression in a an expression tree represented by node. By "lazy" I mean that a tree should not be entirely flattered into a list like this:
This visitor will traverse entire expression tree - the Visit() method will be called on every node regardless of how much nodes I really want to visit. The task is to minimize the footprint and visit as less nodes as possible.
The lazy visitor should return expressions in the same order as NonLazyVisitor above.
class NonLazyVisitor : ExpressionVisitor
{
readonly
List<Expression>
_nodes = new List<Expression>();
public
static IEnumerable<Expression> All(Expression node)
{
var visitor = new NonLazyVisitor();
visitor.Visit(node);
return visitor._nodes;
}
public
override Expression
Visit(Expression
node)
{
_nodes.Add(node);
return base.Visit(node);
}
}This visitor will traverse entire expression tree - the Visit() method will be called on every node regardless of how much nodes I really want to visit. The task is to minimize the footprint and visit as less nodes as possible.
The lazy visitor should return expressions in the same order as NonLazyVisitor above.