How do I get a custom attribute off a value

B

bg

hi all,

I have this:

// custom attribute
[AttributeUsage(AttributeTargets.Field)]
internal class ThingAttribute : System.Attribute
{ ... }

enum LotsOfThing
{
[ThingAttribute("asd")] < == use custom attribute here
onesingular;
}

// call a method using above ...
c.method(LotsOfThing.onesingular);

void method(LotsOfThing value)
{
ThingAttribute ta = ???? < == how do I get to the custom attribute on
value?
}

This should be really easy but i just can't work out how to get to the
custom attribute on the "value" parameter.

TIA

bg
 
N

Nicholas Paldino [.NET/C# MVP]

bg,

What you have to do is get the type of the enumeration. Once you get
that, you will need to get the fields on the type which are static. These
are the values of the enumeration. Once you have the FieldInfo instances,
you can get the custom attributes from there.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top