如何获取 FxCop 中 callvirt IL 指令实际调用的方法
我仍在努力让我的 FxCop 规则发挥作用。
作为其中的一部分,我需要弄清楚方法调用哪些方法。以前我使用的是 CallGraph.CallersFor() (反向操作,无论如何这是我的最终目标),但它似乎存在我在下面描述的相同问题。
作为使用 CallGraph 类的替代方法,我尝试根据以下代码访问所有方法调用来构建字典:
public override void VisitMethodCall(MethodCall call)
{
Method CalledMethod = (call.Callee as MemberBinding).BoundMember as Method;
// ....
}
但是,事实证明,如果被调用的方法位于重写基类的派生类上, class' 方法,则 BoundMember
是基类' 方法,而不是子类' 方法(这是实际调用的方法)。
问题:如何获取在 FxCop 中的 callvirt IL 指令的情况下将调用的方法?
I'm still trying to get my FxCop rule working.
As part of this, i need to work out what methods a method calls. Previously i was using CallGraph.CallersFor()
(doing it in reverse, which is my final aim anyway), however it appears to have the same issue i describe below.
As an alternative to using the CallGraph
class i tried visiting all method calls to build a dictionary, based on this code:
public override void VisitMethodCall(MethodCall call)
{
Method CalledMethod = (call.Callee as MemberBinding).BoundMember as Method;
// ....
}
However, it turns out that if the called method is on a derived class that overrides a base class' method, then the BoundMember
is the base class' method, not the child class' method (which is the one that will actually be called).
Question: How can i get the method that will be called in the case of a callvirt IL instruction in FxCop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,就我而言,我实际上并不需要这个(这很好,因为在这种情况下我没有确切的答案)。
因为 FxCop 是静态检查器,所以它永远无法知道变量所指向的对象实例的类型,该变量可以声明为基类型。因此我相信我所要求的是不可能的。
我的解决方案是,在构建调用树时,我为基类“调用”任何派生类添加额外的引用。这样,如果我调用基类上的方法,并且可以调用派生类的方法,我可以以这种方式跟踪调用树。
请参阅下面的 FxCop 规则类使用的类:
Turns out in my case that i don't actually need this exactly (which is good because i don't have an exact answer in this case).
Because FxCop is a static checker it can never know the type of the instance of the object pointed to by the variable, which could be declared as a base type. Therefore i believe what i'm asking for is impossible.
My solution here, is to, when building the call tree, i add in extra references for a base class 'calling' any derived classes. In this way if i call a method on the base class, and the derived class' method could be called i can follow the call tree in that way.
See below for my class used by FxCop rule classes: