lakedoo
lakedoo

Reputation: 385

Issue finding custom attributes

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

Upvotes: 1

Views: 513

Answers (3)

jk7
jk7

Reputation: 2100

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.

Upvotes: 1

Praveen Paulose
Praveen Paulose

Reputation: 5771

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

Upvotes: 2

CodingWithSpike
CodingWithSpike

Reputation: 43718

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.

Upvotes: 2

Related Questions