Enumeration has fields that aren't userfriendly, possible to attachedmeta data to them?

D

DotNetNewbie

Hi,

I am creating the administration side of a web application, and I want
to basically enumerate through a Enumerations items and display the
enum items name and value.

Problem is the name of each item is not user friendly, is it possibel
for me to attach some kind of meta deta that I could access via
reflection?

example:

public enum SomeEnum
{
BrownColor = 1,
GreenColor = 2
}

Since 'browncolor' and 'greencolor' is not written properly (I would
like "brown color"), is there any kind of meta data I could add?

Like:

public enum SomeEnum
{
["Brown Color"]
BrownColr = 2

}

Possible?
 
P

Peter Duniho

[...]
public enum SomeEnum
{
BrownColor = 1,
GreenColor = 2
}

Since 'browncolor' and 'greencolor' is not written properly (I would
like "brown color"), is there any kind of meta data I could add?

I don't know if there's an existing attribute you could apply. There
might be. But if not, you could always define your own attribute class.

That said, depending on what naming convention is being followed in the
enum, maybe it's enough to just insert spaces before capital letters?
That wouldn't require any changes to the type itself...you'd just scan the
value name and when you reach a character for which converting to
lower-case returns a different character than the actual character, stick
a space into the displayed string before that character.

Pete
 
J

Jon Skeet [C# MVP]

Peter Duniho said:
I don't know if there's an existing attribute you could apply. There
might be. But if not, you could always define your own attribute class.

I've used the Description attribute before -
System.ComponentModel.DescriptionAttribute.

<snip>
 
D

DotNetNewbie

I've used the Description attribute before -
System.ComponentModel.DescriptionAttribute.

<snip>

So say you have an enumerator, how could I loop through all the items
and output the Description attribute for each item?
 
J

Jon Skeet [C# MVP]

DotNetNewbie said:
So say you have an enumerator, how could I loop through all the items
and output the Description attribute for each item?

There may well be a better way of doing it, but this certainly works:

using System;
using System.ComponentModel;
using System.Reflection;

enum Colour
{
[Description("Ruby")]
Red,
[Description("Emerald")]
Green,
[Description("Sapphire")]
Blue
}

class Test
{
static void Main()
{
foreach (Colour colour in Enum.GetValues(typeof(Colour)))
{
FieldInfo field = typeof(Colour).GetField
(colour.ToString());

DescriptionAttribute description = (DescriptionAttribute)
Attribute.GetCustomAttribute
(field, typeof(DescriptionAttribute));

Console.WriteLine("{0}={1}",
colour,
description.Description);
}
}
}
 

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