How do I get a custom attribute off a value

  • Thread starter Thread starter bg
  • Start date Start date
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
 
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.
 
Back
Top