2011-02-02 48 views
2

我需要一個枚舉或類似的東西,以做這樣的事情:enum(estring)的字符串表示形式?

公共枚舉MyStringEnum { [的StringValue( 「富A」)]的Foo = 「A」, [的StringValue( 「富B」) ] Foo =「B」}

這是可能的嗎?我的例子,我返回一個數據集的值表示爲A,B,C,D,E ..我需要一個解決方案,以字符串表示形式返回它?

我想顯而易見的是創建一個擴展方法,或者只是有一個switch語句並返回一個字符串..其他更清潔的解決方案?

問候, 戴夫

+0

我應該指出,枚舉的基礎值不能是字符串。 `enum的批准類型是字節,sbyte,short,ushort,int,uint,long或ulong.` http://msdn.microsoft.com/en-us/library/sbbt4032.aspx – Greg 2011-02-02 16:03:13

+0

我想指向你Mark的答案......在我看來,這是非常優雅的解決方案。 – 2013-02-08 13:06:10

回答

1

難道是一個選項不使用枚舉和使用結構呢?

struct FooEnum 
{ 
    private int value; 
    private string name; 
    private FooEnum(int value, string name) 
    { 
     this.name = name; 
     this.value = value; 
    } 

    public static readonly FooEnum A = new FooEnum(0, "Foo A"); 
    public static readonly FooEnum B = new FooEnum(1, "Foo B"); 
    public static readonly FooEnum C = new FooEnum(2, "Foo C"); 
    public static readonly FooEnum D = new FooEnum(3, "Foo D"); 

    public override string ToString() 
    { 
     return this.name; 
    } 

    //TODO explicit conversion to int etc. 
} 

然後,您可以使用FooEnum像一個枚舉與自己的ToString()過載:

FooEnum foo = FooEnum.A; 
string s = foo.ToString(); //"Foo A" 
0

我已經看到了這個地方做我會把

MyStringEnum.Foo.ToString(); 

在這種情況下,將給予「A」

1

如果你想要做這樣的事:

MyStringEnum value = MyStringEnum.A; 
string description = value.GetDescription(); 
// description == "Foo A" 

設置你的枚舉像這樣:

public enum MyStringEnum 
{ 
    [Description("Foo A")] 
    A, 
    [Description("Foo B")] 
    B 
} 

,並使用讀取屬性的實用工具/擴展方法:

public static string GetDescription(this MyStringEnum enumerationValue) 
{ 
    Type type = enumerationValue.GetType(); 
    string name = enumerationValue.ToString(); 

    //Tries to find a DescriptionAttribute for a potential friendly name for the enum 
    MemberInfo[] member = type.GetMember(name); 
    if (member != null && member.Length > 0) 
    { 
     object[] attributes = member[0].GetCustomAttributes(typeof(DescriptionAttribute), false); 

     if (attributes != null && attributes.Length > 0) 
     { 
      //Pull out the description value 
      return ((DescriptionAttribute)attributes[0]).Description; 
     } 
    } 

    return name; 
} 
0

此問題的乾淨的解決方案是創建一個將存儲要用於枚舉常量字符串值自定義屬性。我在過去使用過這個策略,並且效果相當不錯。這裏有一個博客帖子,詳細說明所涉及的工作:

Enum With String Values In C# - Stefan Sedich's Blog

當然,如果你需要某種有意義的文本的這個纔是必需的。如果枚舉常量的名稱適用於您...那麼您可以簡單地調用ToString()

7

下面是我們用於我們的MVC應用程序檢索我們的枚舉的顯示名稱。它使用自定義屬性和擴展方法來檢索枚舉顯示名稱。在枚舉

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] 
public class EnumDisplayNameAttribute : Attribute 
{ 
    public EnumDisplayNameAttribute(string displayName) 
    { 
    DisplayName = displayName; 
    } 

    public string DisplayName { get; set; } 
} 


public static string GetDisplayName(this Enum enumType) 
{ 
    var displayNameAttribute = enumType.GetType() 
            .GetField(enumType.ToString()) 
            .GetCustomAttributes(typeof(EnumDisplayNameAttribute), false) 
            .FirstOrDefault() as EnumDisplayNameAttribute; 

    return displayNameAttribute != null ? displayNameAttribute.DisplayName : Enum.GetName(enumType.GetType(), enumType); 
} 

用法:

public enum Foo 
{ 
    [EnumDisplayName("Foo Bar")] 
    Bar = 0 
} 

再回到顯示名稱:

var f = Foo.Bar; 
var name = f.GetDisplayName(); 
0

我覺得this可以提供幫助。我同意你應該創建UI包裝來枚舉。

相關問題