Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:28:07+00:00 2026-05-23T09:28:07+00:00

I want to write a rule that will fail if an object allocation is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:28:08+00:00Added an answer on May 23, 2026 at 9:28 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a template that will grab a mixture of text nodes
I have some code in a reusable class that modifies some types. Here's a
UPDATE I reflected Microsoft.Cci.dll and build my rule. It works fine. However, I am
1) I have a Drupal site located at http://example.com and I have a directory
I'm using sessions in Django to store login user information as well as some
There must be a simple solution out there, I'm trying to use NHibernate Validator
I'm looking for suggestions for ways to share Android app data between phones running
I'm trying to implement a basic MIME parser for the multipart/related in C++/Qt. So
I tried to modify the mini_c example of boost::spirit to match to my existing
I'm making a classifieds ads website where users submit ads through a form (like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.