简体   繁体   English

在表达式树/迭代表达式树中查找方法调用

[英]Finding method call in expression tree / iterating expression tree

I need to find all calls to particular method in expression tree which can be very complex. 我需要在表达式树中找到对特定方法的所有调用,这可能非常复杂。 Currently I have simple recursive code which checks for BinaryExpression, ConditionalExpression etc and of course for MethodCallExpression. 目前我有简单的递归代码,它检查BinaryExpression,ConditionalExpression等,当然还有MethodCallExpression。 It works but I wonder if there is simpler way? 它有效,但我想知道是否有更简单的方法?

What I need is an iterator for all MethodCallExpression in complex expression so I can just check MethodCallExpression and don't care about other types of expression in my tree. 我需要的是复杂表达式中所有MethodCallExpression的迭代器,所以我可以检查MethodCallExpression而不关心我树中的其他类型的表达式。 Is something like this builtin for example somewhere in System.Linq.Expressions? 是这样的内置例如在System.Linq.Expressions中的某处?

As svick correctly points out: use the ExpressionVisitor base class: 正如svick正确指出:使用ExpressionVisitor基类:

http://msdn.microsoft.com/en-us/library/system.linq.expressions.expressionvisitor.aspx http://msdn.microsoft.com/en-us/library/system.linq.expressions.expressionvisitor.aspx

If you want to roll your own or see how ExpressionVisitor works, in this article Matt Warren shows you how to rebuild an expression tree by visiting every node. 如果您想自己动手或看看ExpressionVisitor的工作原理,在本文中,Matt Warren将向您展示如何通过访问每个节点来重建表达式树。

http://blogs.msdn.com/b/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx http://blogs.msdn.com/b/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx

You don't need to rebuild it, you just need to search it. 您不需要重建它,只需要搜索它。 You can modify Matt's code so that it does not return a new rebuilt expression, it just recursively searches each child node. 您可以修改Matt的代码,使其不返回新的重建表达式,它只是递归搜索每个子节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM