1
votes

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

4
For clarity, can you post an example of an enum like the one you would like to use? - HitLikeAHammer
Why are your enumeration names so long? Typically a enum is simply a mnemonic for the programmer to refer to numerical values in code NOT a means to store string values. Typically retrieving the names is sensibile when giving a user the option to select a given value from the enumeration or for display purposes. - Achilles
@Achilles: Usually the enum will have some descriptive text to go with it, hence the reason for long names. - NotMe
I agree but I've been asked to do it :( public enum PriceIndexType : int { [StringValue("RPI (All Items Excluding Mortgage")] RPI1 = 1, [StringValue("RPI (All Items)")] RPI2 = 2, } I want to store the int value but say when I return a list, I want to display the long value. thanks - Davy
FYI myid.ToString will return the name of the ENum unless myId is not the type of the enum then you just have to type it ((MyEnum)myId).ToString() - David Basarab

4 Answers

5
votes

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.

2
votes

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...

0
votes

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.

0
votes

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.