我在枚举类型中使用Description属性来为枚举字段提供用户友好的名称,例如:
public enum InstallationType
{
[Description("Forward of Bulk Head")]
FORWARD = 0,
[Description("Rear of Bulk Head")]
REAR = 1,
[Description("Roof Mounted")]
ROOF = 2,
}
使用一个好用的助手方法很容易访问这个:
public static string GetDescriptionFromEnumValue(Enum value)
{
DescriptionAttribute attribute = value.GetType()
.GetField(value.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.SingleOrDefault() as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
我需要将它转换为可移植类库,但似乎无法访问System.ComponentModel库。当我尝试添加引用时,VS告诉我已经引用了所有内容。
谢谢
System.ComponentModel.DescriptionAttribute
实际上是System.dll
的一部分,这应该已经是您项目的引用了。您只是缺少了using
指令吗? - olydis