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?
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: