My task is to modify an expression
according to several rules. One of them is to remove the expression of the conversion at the top level, if it is, but only the top, not the internal ExpressionVisitor {Protected Override expressionUnaryExpression node {if (node.NodeType == ExpressionType.Convert: I class < P>
class removeConvertToObjectExpressionVisitor implemented executed || node.NodeType == expression type.convertclaimed) {return base. Visit (node operand); } Return Base Visit Unary (node); }} But it removes all the changing expressions. See the examples below (this is just a sample of one behavior, no more meaning in code).
class model {public int value {received; Set; }} Zero main () {expression & lt; Funk & lt; Model, object & gt; & Gt; Expression = m = & gt; M.Value + int.Parse ((object) "5"). ToString ()); Var visitor = new RemoveConvertToObjectExpressionVisitor (); Var Results = Visur Visit (expression body); }
I want to have the result expression Due to your code ("5" .toString ()) m.value + parse (convert ("5"). ToString ())
, but it returns
Change it via the ExpressionVisitor
implementation is difficult, because you need to tell the visitor from inside whether you are viewing the root expression or not. One way of doing this is defining the bool
variable top level
, setting it to true
in the beginning, and then it will be < Code> set to false inside all your VisitXYZ
methods:
class RemoveConvertToObjectExpressionVisitor: ExpressionVisitor {Private bool High Level = True Protected Override Expression Visit Uni (Unit Expiration Node) {bool currentTop = topLevel; TopLevel = false; If (currentTop & amp; amp; (node.NodeType == ExpressionType.Convert || node.NodeType == ExpressionType.ConvertChecked)) {return base.Visit (node.Operand); } Return Base Visit Unary (node); } /// You need to override all VisitXyz methods with the same code protected override ... (... expression node) {topLevel = false; Return basis Visit (node); }}
This requires a lot of code, and even more effort from the reader to understand what's going on. You will be better off with a simple type of investigation on very top-level expressions, and if you see a conversion by grabbing its sophistication:
Expression of & lt; & Lt; Model, object & gt; & Gt; Expression = m = & gt; M.Value + int.Parse ((object) "5"). ToString ()); Expression Results = Expression Body; Type BoolConvertTopNode = result.NodeType == expression. Convert || result. Node type == Expression type. Convert checked; If (isConvertTopNode) {result = (UnaryExpression expression. Body) .operand; }
Comments
Post a Comment