design patterns - What is the motivation of C# ExpressionVisitor's implementation? -
I have to prepare a solution for the job, and I use something similar to the expression visitor in C # Would like to
For curiosity, I see it for expression visitor . NET source opened. Since that time I was wondering why the .NET team implemented the visitor.
For example, MemberInitExpression.Accept looks like this:
Accept protected internal override expression (Expression Visitor Visitor) {visit visit. VisitMemberInit (this); } My - probably nonbob - the question is is it does not make any sense? I mean that the acceptable method should not be responsible for itself, how does it apply the methods to come within itself? I mean I have expected something like that ( internal visibility is eligible to override from outside):
accept protected override expression (expression visitor visitor) {it Return updates (visitor.visit and convert (visit this new expression, "visitemember init"), visitors. (This biddings, visitmemberbending)); } But this code is in the base Expression Visitor s MemberInitExpression.Accept . It seems that accept implementation is not like any of the benefits here.
Why is not the process of tree in just expression visitor, and forget about all acceptable methods?
I hope that you think of my points, and hope that it can put any light on the motivation behind this implementation. Maybe I'm not considered Visitor Pattern at all? ...
The way a visitor can override has visited any expression. If your proposal was implemented in all places then the visitor will never be asked. All soft arguments will be in the override of accept . Non-BCL codes can not override this method.
If you type visitor.Visit (this expression). Some expression) (as you do in the question), how are you going to do a dynamic transmission on the type of SomeExpression ? Now there is a dynamic dispatch to the visitor. Note that your second code snippet creates a simplified notion that the type of attack on all sub-expressions is a known type. Try to try writing code for BinaryExpression .
Perhaps I did not understand anything but this motion is not understood.
Objective Accept is a performance optimization of the method. Accepting each is a virtual call which is cheaper. For the option type, the expression must have a big switch in the visitor (which is an enum) This is probably slow.
Comments
Post a Comment