1

I am trying to find all the methods in my assembly that have a custom attribute. I have tried the other solutions posted on here, but I cant seem to get any results on which methods have the attribute type I am looking for. Hopefully someone can point out what I am doing wrong.

[AttributeUsage(AttributeTargets.All)]
public class RPCAttributeType : Attribute
{
    private string _name;
    private double _version;

    public double Version
    {
        get { return _version; }
        set { _version = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public RPCAttributeType(string name)
    {
        Name = name;
        Version = 1.0;
    }
}


    public RPCModule()
    {
        try
        {



            Assembly assembly = Assembly.ReflectionOnlyLoad("Parser");


            var results = CustomAttributeData.GetCustomAttributes(assembly);

            ShowAttributeData(results);


        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);

        }

    }

    [RPCAttributeType("Select")]
    public DataRow[] Select(string expression)
    {
        return MessageDataTable.Select(expression);
    }
lakedoo
  • 385
  • 1
  • 4
  • 13
  • "I've tried the other solutions..." Care to show us which? I came up with 2 dozen different ones and I didn't even narrow my criteria. How about this one? http://stackoverflow.com/questions/3467765/get-method-details-using-reflection-and-decorated-attribute – Bob G Mar 31 '15 at 17:37

3 Answers3

2

You can do this to fetch a list of all methods using the RPCAttributeType

var results = assembly.GetTypes().SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(RPCAttributeType), true).Count() > 0).ToList();

EDIT: BASED ON COMMENT

When assigning assembly use

var assembly = Assembly.GetExecutingAssembly();
Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19
  • The other way around. It fetches the methods not the Types and the question clearly asks for "all methods in the assembly that have a custom attribute" – Praveen Paulose Mar 31 '15 at 17:52
  • when I try your solution it throws InvalidOperationException with this error: `"It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType (see Assembly.ReflectionOnly) -- use CustomAttributeData instead."` – jk7 Mar 31 '15 at 18:48
  • Use assembly = Assembly.GetExecutingAssenbly() – Praveen Paulose Mar 31 '15 at 18:51
  • Yes, it works if `var assembly = Assembly.GetExecutingAssenbly();` is used. You might add that to your answer. Also, to get a specific assembly, `var assembly = typeof(Parser).Assembly;` works - using the OP's example where the class name is "Parser". – jk7 Mar 31 '15 at 19:30
2

Your code is only checking for assembly-level attributes.

Since your attribute can be applied to classes, methods, properties, etc you would need to loop through all these things with reflection to locate your attribute.

Conceptually something like...

foreach assembly
    foreach type
        foreach property
        foreach method
        foreach field

However, your question states "find all the methods in my assembly" so if you are only planning on applying this attribute to methods, then you should change

[AttributeUsage(AttributeTargets.All)]

to:

[AttributeUsage(AttributeTargets.Method)]

Then you could use Praveen's solution to just check methods in all types and not worry about properties, fields, etc.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
1

Using ReflectionOnlyLoad of the original example, it can be done like this:

Assembly assembly = Assembly.ReflectionOnlyLoad("Parser");
var results = assembly.GetTypes()
              .SelectMany(t => t.GetMethods())
              .SelectMany(m => CustomAttributeData.GetCustomAttributes(m)
              .Where(c => c.AttributeType.FullName == typeof(RPCAttributeType).FullName))
              .ToList();
ShowAttributeData(results);

NB: This populates the results variable with a list of CustomAttributeData for the specified custom attribute, like the original code example, not a list of MethodInfo for methods containing the custom attribute as described in the original question.

jk7
  • 1,958
  • 1
  • 22
  • 31