Hi,
I have an occasion where it would be useful to add attributes to
individual enum members, and I can do that very well enough, the
problem is: how do I access them "after the fact"?
the obvious
Thing foo = Thing.Bar;
object[] attrs = foo.GetType().GetCustomAttributes(false);
does not work for obvious reasons..
Do I have to rethink my approach to the problem (i.e. skip the use of
attributes on enum members), or is there a way to make it work? 5 8041
IIRC, they manifest as static fields obtainable via reflection:
enum TestEnum { A, B, C }
static void Main()
{
FieldInfo fi = typeof(TestEnum).GetField("A");
}
From here, the FieldInfo has an Attributes property
Marc
FieldInfo fi = typeof(TestEnum).GetField("A");
thank you!
that was the pointer I needed to get it working.. (I still needed
fi.GetCustomAttributes(...) though)
/Simon
Oops; yep GetCustomAttributes is the way... I was thinking of
PropertyDescriptor where "Attributes" is the set of custom attributes; for
the reflection {blah}Info classes you are entirely correct. I guess I've
been messing with the component-model too much lately ;-p
Oh well, I got the static fields bit right!
Marc
Simon,
I have an occasion where it would be useful to add attributes to
individual enum members,
Could you post an example of an enum that has attributes added please?
It sounds like something that might be quite useful, but I've not come
across it before.
Cheers,
Richard
example with attributes; note that they won't be used by the component
model, so you would need to explicitely make use of the attributes
yourself...
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
public class EnumDisplayNameAttribute : DisplayNameAttribute {
public EnumDisplayNameAttribute(string displayName) : base(displayName)
{ }
}
enum TestEnum
{
[EnumDisplayName("Test A")]
TestA,
[EnumDisplayName("Test B")]
TestB,
[EnumDisplayName("Test C")]
TestC
}
class Test
{
static void Main()
{
foreach(FieldInfo fi in typeof(TestEnum).GetFields()) {
string displayName = fi.Name;
foreach (EnumDisplayNameAttribute attrib in
fi.GetCustomAttributes(typeof(EnumDisplayNameAttri bute), true))
{
displayName = attrib.DisplayName;
break;
}
Debug.WriteLine(fi.Name + "\t" + displayName);
}
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bilgehan.Balban |
last post by:
Hi,
I have two different enum definitions with members of same name. The
compiler complains about duplicate definitions. Is this expected
behaviour?
Can't I have same-named fields in...
|
by: Nikhil Patel |
last post by:
Hi all,
I am a VB6 programmer and learning C#. I am currently reading a chapter
on types. I have question regarding enums. Why do we need to convert enum
members to the value that they represent?...
|
by: David |
last post by:
Hi,
namespace EnumTest
{
class Class1
{
public enum num
{
one = 1,
uno = 1,
|
by: Ken Allen |
last post by:
I have a need to convert the names of the members of an enumeration into an
array of strings at runtime.
I have determined a method using reflection.
Type theType = typeof(MyEnumName);...
|
by: Michael.McD |
last post by:
HI,
I'd like to wire an enum's properties to a table in a dB at runtime.
Has any one here tried this? Is it possible? And if it is how is it done?
Thanks in advance,
Michael McDowell
|
by: Samantha |
last post by:
Hi - I am seeing enum members being used, with just the member's
name, and not the enum name.
for example:
typedef enum Fruit
{
Apple = 0,
Orange,
Banana = 0xFF
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |