我是否缺少在枚举上使用 [Display(Name = "string")] 的用法?

Am I missing a using to use [Display(Name = "string")] on an enum?

"The type or namespace name Display cannot be found ... " "The type or namespace name Name cannot be found ... "

public enum MyEnum
{
  [Display(Name = "The First")]
  first = 1,
  [Display(Name = "The Second")]
  second,
  [Display(Name = "The Third")]
  third= 1
}

我尝试了 [DisplayName("The First")],但仅对 Class、方法 属性 或事件有效...

我在这里错过了什么?

UDPATE:需要[Description()] 所以...

public enum MyEnum
{
  [Description("The First")]
  first = 1,
  [Description("The Second")]
  second,
  [Description("The Third")]
  third= 1
}

您可能想使用说明。

public enum MyEnum
{
  [Description(Name = "The First")]
  first = 1,
  [Description(Name = "The Second")]
  second =2,
  [DisDescriptionplay(Name = "The Third")]
  third= 1
}

如果有人因为 正在 寻找 DisplayAttribute 而最终来到这里,它位于 System.ComponentModel.DataAnnotations 命名空间中。让事情变得混乱的是你可以为这个特定的命名空间添加一个 using 指令,它不会抱怨......但它也不会找到 DisplayAttribute 。它保持灰色,好像该指令是不必要的。

右键单击项目中的 "References",然后 select "System.ComponentModel.DataAnnotations"

using指令会变黑,Display属性会被识别。