By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,770 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,770 IT Pros & Developers. It's quick & easy.

How do I get a custom attribute off a value

P: n/a
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
May 12 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"bg" <bg@bg.com> wrote in message
news:OI**************@TK2MSFTNGP04.phx.gbl...
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

May 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.