如何使用包含空格的枚舉項目名稱?如何使用包含空格的枚舉項目名稱?
enum Coolness
{
Not So Cool = 1,
VeryCool = 2,
Supercool = 3
}
我對下面的代碼
string enumText = ((Coolness)1).ToString()
我不慣於改變這種代碼,但是上面的代碼應該返回不是很爽獲取枚舉項目名稱。 有沒有使用oops概念來實現這一點? 這裏我不想改變檢索語句。您枚舉
如何使用包含空格的枚舉項目名稱?如何使用包含空格的枚舉項目名稱?
enum Coolness
{
Not So Cool = 1,
VeryCool = 2,
Supercool = 3
}
我對下面的代碼
string enumText = ((Coolness)1).ToString()
我不慣於改變這種代碼,但是上面的代碼應該返回不是很爽獲取枚舉項目名稱。 有沒有使用oops概念來實現這一點? 這裏我不想改變檢索語句。您枚舉
避免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);
你不能枚舉名稱空間。所以要麼刪除它們,而要用_
之類的東西替換它們。
但如果名稱是非常重要的,你也可以使用自定義類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;
}
}
你會發現使用說明另一種方法屬性在這裏:
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的助手)
刪除空格,他們是酷 –
沒錯,它不是possible.to有承包商,客人的枚舉與空間。 – pwas
也許你需要[用字符串枚舉](http://stackoverflow.com/questions/630803/associating-enums-with-strings-in-c-sharp) – tomab