Can you access a long description for a specific enum value

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


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.


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


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.

链接地址: http://www.djcxy.com/p/91542.html

上一篇: 如何获得枚举值属性的IEnumerable <string>?

下一篇: 你能访问一个特定枚举值的长描述吗?