如何使用包含空格的枚举项名称?

时间:2015-10-13 11:43:52

标签: c# .net oop enums

如何使用包含空格的枚举项目名称?

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

我通过以下代码获取Enum项目名称

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

我不会更改此代码,但上面的代码应该返回Not So Cool。 有没有使用oops概念来实现这一目标? 在这里,我不想更改检索声明。

3 个答案:

答案 0 :(得分:5)

使用Display attributes

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

您可以使用此帮助程序获取DisplayName

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

答案 1 :(得分:1)

在枚举上避免使用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);

答案 2 :(得分: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?

相关问题