> [...]
> public enum SomeEnum
[quoted text clipped - 5 lines]
> 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
Jon Skeet [C# MVP] - 06 Mar 2008 07:46 GMT
> > 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.
I've used the Description attribute before -
System.ComponentModel.DescriptionAttribute.
<snip>

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
DotNetNewbie - 06 Mar 2008 22:11 GMT
> > > Since 'browncolor' and 'greencolor' is not written properly (I would
> > > like "brown color"), is there any kind of meta data I could add?
[quoted text clipped - 10 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk
So say you have an enumerator, how could I loop through all the items
and output the Description attribute for each item?
Jon Skeet [C# MVP] - 06 Mar 2008 23:15 GMT
> 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);
}
}
}

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk