确定方法是否调用包含新语句的另一个程序集中的方法,反之亦然

发布于 2024-11-16 18:30:26 字数 1222 浏览 3 评论 0原文

我想编写一个规则,如果在标记有特定属性的方法调用的任何方法中进行对象分配,该规则将会失败。

到目前为止,我已经完成了这项工作,通过迭代调用我的方法的所有方法来使用 CallGraph.CallersFor() 进行检查,以查看这些父方法中是否有任何一个具有该属性。

这适用于检查与要检查的方法相同的程序集中的父方法,但是在线阅读时,似乎有一次 CallGraph.CallersFor() 确实查看了所有程序集,但现在它没有。

问题:有没有办法获取调用给定方法的方法列表,包括不同程序集中的方法?

替代答案:如果上述不可能,我如何循环遍历给定方法调用的每个方法,包括不同程序集中的方法。


示例:

-----In Assembly A

public class ClassA
{
    public MethodA()
    {
        MethodB();
    }

    public MethodB()
    {
        object o = new object(); // Allocation i want to break the rule
        // Currently my rule walks up the call tree,
        // checking for a calling method with the NoAllocationsAllowed attribute.
        // Problem is, because of the different assemblies,
        // it can't go from ClassA.MethodA to ClassB.MethodB.
    }
}


----In Assembly B

public var ClassAInstance = new ClassA();

public class ClassB
{
    [NoAllocationsAllowed] // Attribute that kicks off the rule-checking.
    public MethodA()
    {
        MethodB();
    }

    public MethodB()
    {
        ClassAInstance.MethodA();
    }
}

我并不介意规则在哪里报告错误,在这个阶段获取错误就足够了。

I want to write a rule that will fail if an object allocation is made within any method called by a method marked with a particular attribute.

I've got this working so far, by iterating up all methods calling my method to check using CallGraph.CallersFor(), to see if any of those parent methods have the attribute.

This works for checking parent methods within the same assembly as the method to be checked, however reading online, it appears that at one time CallGraph.CallersFor() did look at all assemblies, however now it does not.

Question: Is there a way of getting a list of methods that call a given method, including those in a different assembly?

Alternative Answer: If the above is not possible, how do i loop through every method that is called by a given method, including those in a different assembly.


Example:

-----In Assembly A

public class ClassA
{
    public MethodA()
    {
        MethodB();
    }

    public MethodB()
    {
        object o = new object(); // Allocation i want to break the rule
        // Currently my rule walks up the call tree,
        // checking for a calling method with the NoAllocationsAllowed attribute.
        // Problem is, because of the different assemblies,
        // it can't go from ClassA.MethodA to ClassB.MethodB.
    }
}


----In Assembly B

public var ClassAInstance = new ClassA();

public class ClassB
{
    [NoAllocationsAllowed] // Attribute that kicks off the rule-checking.
    public MethodA()
    {
        MethodB();
    }

    public MethodB()
    {
        ClassAInstance.MethodA();
    }
}

I don't really mind where the rule reports the error, at this stage getting the error is enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2024-11-23 18:30:26

我通过在 FxCop 项目中添加所有引用的 dll 并使用下面的代码来解决这个问题,该代码手动构建调用树(它还添加对派生类的调用来解决我遇到的另一个问题,此处

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;
    }
}

I got round this issue by adding all referenced dlls in my FxCop project, and using the code below, which builds a call tree manually (it also adds calls for derived classes to work round another problem i encountered, here.

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;
    }
}
临风闻羌笛 2024-11-23 18:30:26

你有没有尝试过这样的方式

    StackTrace stackTrace = new StackTrace();
    MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
    object [] items = methodBase.GetCustomAttributes(typeof (NoAllocationsAllowed));
    if(items.Length > 0)
        //do whatever you want! 

Did you give it a try in this way,

    StackTrace stackTrace = new StackTrace();
    MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
    object [] items = methodBase.GetCustomAttributes(typeof (NoAllocationsAllowed));
    if(items.Length > 0)
        //do whatever you want! 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文