代码之家  ›  专栏  ›  技术社区  ›  Davy

可以访问特定枚举值的长描述吗

  •  1
  • Davy  · 技术社区  · 15 年前

    我通常访问对应值的枚举描述,例如:

    enum.getname(typeof(myenum),myid);

    我需要一个枚举,可以使用任何字符,如“hello world%^$

    我见过有人附加了一个属性并添加了如下扩展:

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

    但我不知道这是否可以用于访问长描述。

    有人做过类似的事吗?

    谢谢

    戴维

    4 回复  |  直到 15 年前
        1
  •  5
  •   David Basarab    15 年前

    为什么不行?

    可以通过从属性插入来创建自己的属性

    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;
    }
    

    您的枚举将如下所示

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

    你可以这样用

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

    它输出

    test1.getLongDescription=这是数字1

    test1.getshortdescription=1

    实际上,您可以向属性添加属性以包含各种信息。然后您可以支持您要查找的本地化。

        2
  •  2
  •   Jon Skeet    15 年前

    “长描述”是什么意思?我有一个 library 它允许你附加 Description 枚举值的属性并获取它们:

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

    如果您谈论的是XML文档,那是另一回事——它没有内置到二进制文件中,因此您还必须构建/传送XML,然后在执行时获取它。这是可行的,但我没有现成的代码…

        3
  •  0
  •   Dave    15 年前

    我倾向于远离这种做法。如果您考虑一下,它将您的代码逻辑绑定到您如何键入代码上。最好使用switch语句、资源文件、数据库等。

    我很难学会这一点。我有一个应用程序,我们最终决定模糊,以帮助保护我们的代码。正如您所能想象的那样,由于枚举在混淆期间被重命名,我们的二进制文件停止了我们想要的工作方式。

        4
  •  0
  •   Community Rick James    7 年前

    如果需要一种简单的方法来提取枚举值的描述属性,请查看 my answer to a similar question

    你只要打电话给 GetDescription 值的扩展方法:

    string description = myEnumValue.GetDescription();
    

    这个 Unconstrained Melody jon提到的库包含类似的扩展方法(除了许多其他很酷的方法),您应该检查一下。