110,534
社区成员
发帖
与我相关
我的任务
分享
public static string GetDescription(Enum value, Boolean nameInstead = true) {
try {
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null) {
return null;
}
System.Reflection.FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstead == true) {
return name;
}
return attribute?.Description;
} catch {
return string.Empty;
}
}
return attribute?.Description;
//等效于以下代码
if (attribute != null){
return attribute.Description;
}
else{
return null;
}