66

I have a custom attribute:

public class MenuItemAttribute : Attribute
{
}

and a class with a few methods:

public class HelloWorld
{
    [MenuItemAttribute]
    public void Shout()
    {
    }

    [MenuItemAttribute]
    public void Cry()
    {
    }

    public void RunLikeHell()
    {
    }
}

How can I get only the methods that are decorated with the custom attribute?

So far, I have this:

string assemblyName = fileInfo.FullName;
byte[] assemblyBytes = File.ReadAllBytes(assemblyName);
Assembly assembly = Assembly.Load(assemblyBytes);

foreach (Type type in assembly.GetTypes())
{
     System.Attribute[] attributes = System.Attribute.GetCustomAttributes(type);

     foreach (Attribute attribute in attributes)
     {
         if (attribute is MenuItemAttribute)
         {
             //Get me the method info
             //MethodInfo[] methods = attribute.GetType().GetMethods();
         }
     }
}

What I need now is to get the method name, the return type, as well as the parameters it accepts.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
stoic
  • 4,700
  • 13
  • 58
  • 88

3 Answers3

114

Your code is completely wrong.
You are looping through every type that has the attribute, which will not find any types.

You need to loop through every method on every type and check whether it has your attribute.

For example:

var methods = assembly.GetTypes()
                      .SelectMany(t => t.GetMethods())
                      .Where(m => m.GetCustomAttributes(typeof(MenuItemAttribute), false).Length > 0)
                      .ToArray();
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • am i doing it ass-about-front here? cause right now i look for all the attributes and i then try and get the associated method – stoic Aug 12 '10 at 12:52
  • I tried it with your classes; it works for me. Are you searching the right assembly? – SLaks Aug 12 '10 at 13:06
  • doh... changed some of the code to make it simple for stackoverflow readers... never changed it back... thanx... yours is correct – stoic Aug 12 '10 at 13:20
  • @Tom: `GetCustomAttributes()` returns an array anyway, so `.Any()` won't help. – SLaks Nov 06 '12 at 14:55
  • use Assembly.GetExecutingAssembly() for the currently executing assembly – Sachin Joseph Feb 22 '17 at 20:08
  • "You need to loop through every method" is true, but then your example uses `GetMethods()` which doesn't return every method, it only returns public ones. – Ben Voigt Apr 28 '22 at 17:52
25
Dictionary<string, MethodInfo> methods = assembly
    .GetTypes()
    .SelectMany(x => x.GetMethods())
    .Where(y => y.GetCustomAttributes().OfType<MethodAttribute>().Any())
    .ToDictionary(z => z.Name);
JordanBean
  • 1,949
  • 21
  • 28
6
var classType = new ClassNAME();
var methods = classType.GetType().GetMethods().Where(m=>m.GetCustomAttributes(typeof(MyAttribute), false).Length > 0)
.ToArray();

Now you have all methods with this attribute MyAttribute in class classNAME. You can invoke it anywhere.

public class ClassNAME
{
    [MyAttribute]
    public void Method1(){}

    [MyAttribute]
    public void Method2(){}

    public void Method3(){}
}

class MyAttribute: Attribute { }
Matt
  • 25,467
  • 18
  • 120
  • 187
Unknown Artist
  • 143
  • 1
  • 9