我在枚举中使用Description属性为枚举字段提供用户友好名称.例如
public enum InstallationType
{
[Description("Forward of Bulk Head")]
FORWARD = 0,
[Description("Rear of Bulk Head")]
REAR = 1,
[Description("Roof Mounted")]
ROOF = 2,
}
Run Code Online (Sandbox Code Playgroud)
使用一个很好的帮助方法很容易访问它:
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;
}
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为可移植的类库,但它似乎无法访问System.ComponentModel库.当我尝试添加一个崇敬的VS时告诉我,我已经引用了所有内容.
谢谢