Question: In C#, when I call a method, I want to be able to detect if it will (or could potentially) call something with a certain attribute. 

For example, when, TheProgram.Run() get's called, I want to know that it will call a MyClass.DoTheWork, which has an attribute [IsRegistered], which calls a private method FormatTheResult() which also has the attribute [IsRegistered].

I've been thinking about it for a while and can't think how it could be achieved. I'm thinking, something like the invert of a stack trace, or registering components with an attribute or aspect, or perhaps leaning on MEF.

Is this possible?

This detection could happen at compile time or a run time, but ideally before the method with the attribute is executed.

Answer: 
What you are probably looking for is Roslyn.

<http://msdn.microsoft.com/en-au/vstudio/roslyn.aspx>

What you can do with this is analize the syntax tree directly, so for your method in question you could access from the syntax tree all method calls that occur. Then you can follow that and check the method being called has that attribute.

Is pretty complex stuff, so I wont attempt a code sample for your particular scenario but I have used it before to analize multiple solitions and inject code.

It's pretty awesome here is a sample from the docs.

```
namespace GettingStartedCS
{
    class Program
    {
        static void Main(string[] args)
        {
            SyntaxTree tree = SyntaxTree.ParseCompilationUnit(
@"using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(""Hello, World!"");
        }
    }
}");

            var root = (CompilationUnitSyntax)tree.GetRoot();

            var firstMember = root.Members[0];

            var helloWorldDeclaration = (NamespaceDeclarationSyntax)firstMember;

            var programDeclaration = (TypeDeclarationSyntax)helloWorldDeclaration.Members[0];

            var mainDeclaration = (MethodDeclarationSyntax)programDeclaration.Members[0];

            var argsParameter = mainDeclaration.ParameterList.Parameters[0];

        }
    }
}

```
This is the way I've found to do it:

```
public static IList<MethodBase> GetCalledMethods(MethodBase methodBase)
{
    IList<MethodBase> calledMethods = new List<MethodBase>();
    var body = methodBase.GetMethodBody();
    Module module = Assembly.GetExecutingAssembly().ManifestModule;
    byte[] bytes = body.GetILAsByteArray();
    using (var stream = new MemoryStream(bytes))
    {
        long streamLength = stream.Length;
        using (var reader = new BinaryReader(stream))
        {
            while (reader.BaseStream.Position < streamLength)
            {
                byte instruction = reader.ReadByte();
                if (instruction == OpCodes.Call.Value
                    || instruction == OpCodes.Callvirt.Value
                    || instruction == OpCodes.Newobj.Value)
                {
                    int token = reader.ReadInt32();
                    var method = module.ResolveMethod(token);
                    calledMethods.Add(method);
                }
            }
        }
    }

    return calledMethods;
}

```