I have to design a solution for a task, and I would like to use something theoretically similar to C#'s ExpressionVisitor.
For curiosity I opened the .NET sources for ExpressionVisitor
to have a look at it. From that time I've been wondering why the .NET team implemented the visitor as they did.
For example MemberInitExpression.Accept
looks like this:
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitMemberInit(this);
}
My - probably noob - question is: does it make any sense? I mean shouldn't the Accept method itself be responsible of how it implements the visiting within itself? I mean I've expected something like this (removing the internal
visibility to be overridable from outside):
protected override Expression Accept(ExpressionVisitor visitor) {
return this.Update(
visitor.VisitAndConvert(this.NewExpression, "VisitMemberInit"),
visitor.Visit(this.Bindings, VisitMemberBinding)
);
}
But this code is in the base ExpressionVisitor
's VisitMemberInit
method, which gets called from MemberInitExpression.Accept
. So seems like not any benefit of the Accept
implementation here.
Why not just process the tree in the base ExpressionVisitor
, and forget about all the Accept
methods?
I hope you understand my points, and hope someone could shed some light on the motivation behind this implementation. Probably I don't understand the Visitor pattern at all?...