Subtree substitution thanks to microsoft ExpressionVisitor
Previous part: Part1, Part2, Part3.
One of the most important feature of the microsoft ExpressionVisitor I’ve told you before is the ability to substitute expression. Let’s for example see how the VisitBinary is implemented.
|
|
As you can notice, in the line 6 the code is checking if the subtree returned from the left or right branch evaluation are different from the original one, if yes it simply substitute the expression. This is important because we can build visitor that actually replaces part of the tree making traversal simplier. One of the most interesing application is given in this looooong tutorial. Here the author use extensively this feature to build a mini linq to sql provider.
The rule of thumb is, it is not necessary to walk the Expression Tree only one time, it is possibile to traverse it multiple times, each time modify to deal in each pass with a simplier tree.
alk.