8

特定の属性でマークされたメソッドによって呼び出されたメソッド内でオブジェクトの割り当てが行われた場合に失敗するルールを作成したいと考えています。

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

ルールがどこでエラーを報告するかはあまり気にしません。この段階では、エラーを取得するだけで十分です。

4

2 に答える 2

2

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;
    }
}
于 2011-06-27T09:15:24.647 に答える
0

このように試してみましたか?

    StackTrace stackTrace = new StackTrace();
    MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
    object [] items = methodBase.GetCustomAttributes(typeof (NoAllocationsAllowed));
    if(items.Length > 0)
        //do whatever you want! 
于 2011-06-26T09:21:07.513 に答える