views:

202

answers:

4

I usually access enum description for a corresponding value like:

Enum.GetName(typeof(MyEnum), myid);

I need to have an enum that could use any chars like "hello world %^$£%&"

I've seen people attaching an attribute and adding extensions like here:

http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx

but I can't work out if this can be used to access the long description.

Anyone done anything similar?

Thanks

Davy

+2  A: 

What do you mean by "long description"? I've got a library which allows you to attach Description attributes to enum values and fetch them:

public enum Foo
{
    [Description("This is a really nice piece of text")]
    FirstValue,
    [Description("Short but sweet")]
    Second,
}

If you're talking about the XML documentation, that's a different matter - that doesn't get built into the binary, so you'd have to build/ship the XML as well, and then fetch it at execution time. That's doable, but I don't have code to do it offhand...

Jon Skeet
Why do you wiki this?
Jay Riggs
@Jay: I'm making all my posts CW until Monday as a sort of celebratory measure.
Jon Skeet
I store an int in DB. How do I then read that value from a DB, then get the corresponding "This is a really nice piece of text bit" to display for that row instead of "FirstValue"?
Davy
@Davy you have to cast it to the Enum which is real easy, it is (MyEnum)myId == MyEum
David Basarab
Thanks Dave - return ((PriceIndexType)this.PriceIndexId).GetStringValue(); is exactly what I was looking for.
Davy
+5  A: 

Why can't it work out?

You can create your own attribute by inherting from Attribute

public class EnumInformation: Attribute
{
    public string LongDescription { get; set; }
    public string ShortDescription { get; set; }
}

public static string GetLongDescription(this Enum value) 
{
    // Get the type
    Type type = value.GetType();

    // Get fieldinfo for this type
    FieldInfo fieldInfo = type.GetField(value.ToString());

    // Get the stringvalue attributes
    EnumInformation [] attribs = fieldInfo.GetCustomAttributes(
        typeof(EnumInformation ), false) as EnumInformation [];

    // Return the first if there was a match.
    return attribs != null && attribs.Length > 0 ? attribs[0].LongDescription : null;
}

public static string GetShortDescription(this Enum value) 
{
    // Get the type
    Type type = value.GetType();

    // Get fieldinfo for this type
    FieldInfo fieldInfo = type.GetField(value.ToString());

    // Get the stringvalue attributes
    EnumInformation [] attribs = fieldInfo.GetCustomAttributes(
        typeof(EnumInformation ), false) as EnumInformation [];

    // Return the first if there was a match.
    return attribs != null && attribs.Length > 0 ? attribs[0].ShortDescription : null;
}

Your Enum would look like this

public enum MyEnum
{
    [EnumInformation(LongDescription="This is the Number 1", ShortDescription= "1")]
    One,
    [EnumInformation(LongDescription = "This is the Number Two", ShortDescription = "2")]
    Two
}

You can use it this way

MyEnum test1 = MyEnum.One;

Console.WriteLine("test1.GetLongDescription = {0}", test1.GetLongDescription());
Console.WriteLine("test1.GetShortDescription = {0}", test1.GetShortDescription());

It outputs

test1.GetLongDescription = This is the Number 1

test1.GetShortDescription = 1

You can actually add properties to the attribute to have all kinds of information. Then you could support the localization you are looking for.

David Basarab
I have one but that allows me only to say MyEnum,MyDesc.GetstringValue() as in the link I attached. i would then need to do something messy like:if (Enum.GetName(typeof(MyEnum), myid) == "RPI1" return PriceIndex.RPI1.GetstringValue();else if (Enum.GetName(typeof(MyEnum), myid) == "RPI2" return PriceIndex.RPI2.GetstringValue();Messy :(
Davy
See my edit, you can access it right form the type.
David Basarab
In your example of the enum, it says: [LongDescription(LongDescription="This is the Number 1", ShortDescription= "1")]. Shouldn't this be [EnumInformation(LongDescription="This is the Number 1", ShortDescription= "1")]?
Dan Atkinson
@Dan Atinson: Fixed thanks for catching,
David Basarab
A: 

I tend to stay away from this practice. If you think about it, it's binding your code's logic to how you typed your code. It would be much better to use a switch statement, resource file, database, etc...

I learned this the hard way. I had an app that we ultimately decided to obfuscate to help secure our code. As you can imagine, our binaries stopped working the way we wanted due to the enums be renamed during the obfuscation.

Dave
thanks - I do agree but have been asked to do - will show these helpful responses to the boss :)
Davy
A: 

If you need an easy way to extract a description attribute for an enum value, have a look at my answer to a similar question

You just need to call the GetDescription extension method on the value :

string description = myEnumValue.GetDescription();

The Unconstrained Melody library mentioned by Jon includes a similar extension method (among many other cool things), you should check it out.

Thomas Levesque