2015-10-13 88 views
-2

如何使用包含空格的枚舉項目名稱?如何使用包含空格的枚舉項目名稱?

enum Coolness 
{ 
    Not So Cool = 1, 
    VeryCool = 2, 
    Supercool = 3 
} 

我對下面的代碼

string enumText = ((Coolness)1).ToString() 

我不慣於改變這種代碼,但是上面的代碼應該返回不是很爽獲取枚舉項目名稱。 有沒有使用oops概念來實現這一點? 這裏我不想改變檢索語句。您枚舉

+5

刪除空格,他們是酷 –

+0

沒錯,它不是possible.to有承包商,客人的枚舉與空間。 – pwas

+0

也許你需要[用字符串枚舉](http://stackoverflow.com/questions/630803/associating-enums-with-strings-in-c-sharp) – tomab

回答

0

避免space

enum Coolness : int 
{ 
    NotSoCool = 1, 
    VeryCool = 2, 
    Supercool = 3 
} 

要獲得文本的價值,試試這個:

string enumText = ((Coolness)1).ToString() 

如果你想爲枚舉的每個項目一個友好的描述,請嘗試使用Description屬性,對於樣本:

enum Coolness : int 
{ 
    [Description("Not So Cool")] 
    NotSoCool = 1, 

    [Description("Very Cool")] 
    VeryCool = 2, 

    [Description("Super Cool")] 
    Supercool = 3 
} 

並閱讀此屬性,您可以使用的方法是這樣的:

public class EnumHelper 
{ 
    public static string GetDescription(Enum @enum) 
    { 
     if (@enum == null) 
      return null; 

     string description = @enum.ToString(); 

     try 
     { 
      FieldInfo fi = @enum.GetType().GetField(@enum.ToString()); 

      DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 

      if (attributes.Length > 0) 
       description = attributes[0].Description; 
     } 
     catch 
     { 
     } 

     return description; 
    } 
} 

並使用它:

string text = EnumHelper.GetDescription(Coolness.SuperCool); 
+0

我不會這樣。這裏enumText將是「NotSoCool」,但我想「不那麼酷」 – Surendra

+0

我編輯了我的anwser,看看我的編輯。 –

+0

是啊,你是對的這種方式,它會工作.. 但在這裏我不想改變這個語句代碼 string enumText =((Coolness)1).ToString()雖然它應該給我想要的答案。 – Surendra

0

你不能枚舉名稱空間。所以要麼刪除它們,而要用_之類的東西替換它們。

但如果名稱是非常重要的,你也可以使用自定義類Coolness它使用一個enum限制值:

public enum CoolnessFactor:byte { 
    Unknown = 0, 
    NotSoCool = 1, 
    VeryCool = 2, 
    Supercool = 3, 
} 

public class Coolness 
{ 
    private Dictionary<CoolnessFactor, string> CoolnessNames = new Dictionary<CoolnessFactor, string> 
    { 
     { CoolnessFactor.Unknown, "Unknown"}, 
     { CoolnessFactor.NotSoCool, "Not So Cool"}, 
     { CoolnessFactor.VeryCool, "Very Cool"}, 
     { CoolnessFactor.Supercool, "Supercool" } 
    }; 
    public Coolness() : this(CoolnessFactor.Unknown) { } 
    public Coolness(CoolnessFactor factor) 
    { 
     this.Factor = factor; 
    } 

    public CoolnessFactor Factor { get; set; } 

    public byte Value 
    { 
     get { return (byte)Factor; } 
     private set { this.Factor = (CoolnessFactor)value; } 
    } 

    public string Description 
    { 
     get { return CoolnessNames[this.Factor]; } 
    } 
    // other properties and methods... 
    public override string ToString() 
    { 
     return Description; 
    } 

    // just for the sake of completeness 
    public override bool Equals(object obj) 
    { 
     Coolness c = obj as Coolness; 
     return c != null && Value == c.Value; 
    } 
    public override int GetHashCode() 
    { 
     return Value; 
    } 
} 

你會發現使用說明另一種方法屬性在這裏:

Can my enums have friendly names?

5

使用Display attributes

enum Coolness : byte 
{ 
    [Display(Name = "Not So Cool")] 
    NotSoCool = 1, 
    VeryCool = 2, 
    Supercool = 3 
} 

你可以使用這個幫手來獲取顯示名稱

public static string GetDisplayValue(T value) 
{ 
    var fieldInfo = value.GetType().GetField(value.ToString()); 

    var descriptionAttributes = fieldInfo.GetCustomAttributes(
     typeof(DisplayAttribute), false) as DisplayAttribute[]; 

    if (descriptionAttributes == null) return string.Empty; 
    return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString(); 
} 

(感謝Hrvoje Stanisic的助手)

+0

我試圖這樣但 串enumText =((冷寂)1)的ToString() 這個輸出是 「NotSoCool」 – Surendra

+0

此信息http://stackoverflow.com/questions/13099834/how-to-get-通過mvc-razor-code-enum-member-via-mvc-razor-code可以使用enum助手。試試看。 –

+0

這裏我不想改變這個語句代碼 string enumText =((Coolness)1).ToString() – Surendra