关于c#:如何使用包含空格的枚举项名称?

How to use enum item name which contains space?

本问题已经有最佳答案,请猛点这里访问。

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

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

我正在通过下面的代码获取枚举项名称

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

我不想更改这段代码,但上面的代码应该返回不那么酷。有没有使用oops概念来实现这一点?这里我不想更改检索语句。

  • 删除这些空间,它们是不酷的
  • 确切地说,不可能有带空格的枚举成员。
  • 也许你需要带字符串的枚举
  • 您可以添加一个包含空格的DescriptionAttribute
  • 请看一下这个答案,以便为枚举添加描述,以及如何检索这些友好名称:stackoverflow.com/a/1415187/1666620
  • 您可以创建一个类Coolness,其属性为String Namebyte FactorToString返回Name


Use Display attributes:

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

可以使用此帮助器获取显示名称

1
2
3
4
5
6
7
8
9
10
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)

  • 我试过这样做,但字符串enumtext=(cooleness)1.toString()这个输出是"notsocool"
  • 这个post stackoverflow.com/questions/13099834/…有一个枚举助手可以使用。试试看。
  • 这里我不想更改此语句的代码字符串EnumText=(Coolness)1.ToString()。
  • 那么,在这种情况下,你唯一的选择就是重写ToString,让它做点类似return EnumHelper.GetDisplayValue((Coolness)1);的事情。


在你的枚举中避免使用space

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

要获取文本中的值,请尝试以下操作:

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

如果要对枚举的每个项进行友好描述,请尝试使用Description属性作为示例:

1
2
3
4
5
6
7
8
9
10
11
enum Coolness : int
{
    [Description("Not So Cool")]
    NotSoCool = 1,

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

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

要读取此属性,可以使用如下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
    }
}

并使用它:

1
string text = EnumHelper.GetDescription(Coolness.SuperCool);

  • 我不会这样的。这里的EnumText是"NotsCool",但我想要"Not So Cool"
  • 我已经编辑了我的报告,看看我的编辑。
  • 是的,你是对的,这样就行了。但在这里,我不想更改这个语句代码字符串EnumText=(Coolness)1.ToString(),尽管它应该给出所需的答案。


枚举名称中不能有空格。所以要么移除它们,用允许的东西替换它们,比如_

但如果名称如此重要,您也可以使用自定义类Coolness,该类使用enum来限制值:

[cc lang="csharp"]public enum CoolnessFactor:byte {
Unknown = 0,
NotSoCool = 1,
VeryCool = 2,
Supercool = 3,
}

public class Coolness
{
private Dictionary CoolnessNames = new Dictionary
{
{ CoolnessFactor.Unknown,"Unknown