2015-10-13 87 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