【问题标题】:How to get the method actually called by the callvirt IL instruction within FxCop如何获取 FxCop 中 callvirt IL 指令实际调用的方法
【发布时间】:2011-06-23 12:43:52
【问题描述】:

我仍在努力让我的 FxCop 规则发挥作用。

作为其中的一部分,我需要弄清楚一个方法调用了哪些方法。以前我使用的是CallGraph.CallersFor()(反过来,这是我的最终目标),但它似乎有我在下面描述的相同问题。

作为使用 CallGraph 类的替代方法,我尝试访问所有方法调用以构建字典,基于此代码:

public override void VisitMethodCall(MethodCall call)
{
    Method CalledMethod = (call.Callee as MemberBinding).BoundMember as Method;
    // ....
}

然而,事实证明,如果被调用的方法位于覆盖基类方法的派生类上,那么BoundMember 是基类的方法,而不是子类的方法(即实际上会被调用)。

问题:在 FxCop 中的 callvirt IL 指令的情况下,如何获取将被调用的方法?

【问题讨论】:

    标签: c# inheritance fxcop


    【解决方案1】:

    在我的情况下,我实际上并不完全需要这个(这很好,因为在这种情况下我没有确切的答案)。

    因为 FxCop 是一个静态检查器,它永远无法知道变量指向的对象实例的类型,该变量可以声明为基类型。因此我相信我所要求的是不可能的。

    我的解决方案是,在构建调用树时,我为“调用”任何派生类的基类添加额外的引用。这样,如果我在基类上调用一个方法,并且可以调用派生类的方法,我就可以按照这种方式跟踪调用树。

    FxCop 规则类使用的我的类见下文:

    public class CallGraphBuilder : BinaryReadOnlyVisitor
    {
        public Dictionary<TypeNode, List<TypeNode>> ChildTypes;
    
        public Dictionary<Method, List<Method>> CallersOfMethod;
    
        private Method _CurrentMethod;
    
        public CallGraphBuilder()
            : base()
        {
            CallersOfMethod = new Dictionary<Method, List<Method>>();
            ChildTypes = new Dictionary<TypeNode, List<TypeNode>>();
        }
    
        public override void VisitMethod(Method method)
        {
            _CurrentMethod = method;
    
            base.VisitMethod(method);
        }
    
        public void CreateTypesTree(AssemblyNode Assy)
        {
            foreach (var Type in Assy.Types)
            {
                if (Type.FullName != "System.Object")
                {
                    TypeNode BaseType = Type.BaseType;
    
                    if (BaseType != null && BaseType.FullName != "System.Object")
                    {
                        if (!ChildTypes.ContainsKey(BaseType))
                            ChildTypes.Add(BaseType, new List<TypeNode>());
    
                        if (!ChildTypes[BaseType].Contains(Type))
                            ChildTypes[BaseType].Add(Type);
                    }
                }
            }
        }
    
        public override void VisitMethodCall(MethodCall call)
        {
            Method CalledMethod = (call.Callee as MemberBinding).BoundMember as Method;
    
            AddCallerOfMethod(CalledMethod, _CurrentMethod);
    
            Queue<Method> MethodsToCheck = new Queue<Method>();
    
            MethodsToCheck.Enqueue(CalledMethod);
    
            while (MethodsToCheck.Count != 0)
            {
                Method CurrentMethod = MethodsToCheck.Dequeue();
    
                if (ChildTypes.ContainsKey(CurrentMethod.DeclaringType))
                {
                    foreach (var DerivedType in ChildTypes[CurrentMethod.DeclaringType])
                    {
                        var DerivedCalledMethod = DerivedType.Members.OfType<Method>().Where(M => MethodHidesMethod(M, CurrentMethod)).SingleOrDefault();
    
                        if (DerivedCalledMethod != null)
                        {
                            AddCallerOfMethod(DerivedCalledMethod, CurrentMethod);
    
                            MethodsToCheck.Enqueue(DerivedCalledMethod);
                        }
                    }
                }
            }
    
            base.VisitMethodCall(call);
        }
    
        private void AddCallerOfMethod(Method CalledMethod, Method CallingMethod)
        {
            if (!CallersOfMethod.ContainsKey(CalledMethod))
                CallersOfMethod.Add(CalledMethod, new List<Method>());
    
            if (!CallersOfMethod[CalledMethod].Contains(CallingMethod))
                CallersOfMethod[CalledMethod].Add(CallingMethod);
        }
    
        private bool MethodHidesMethod(Method ChildMethod, Method BaseMethod)
        {
            while (ChildMethod != null)
            {
                if (ChildMethod == BaseMethod)
                    return true;
    
                ChildMethod = ChildMethod.OverriddenMethod ?? ChildMethod.HiddenMethod;
            }
    
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多