This commit is contained in:
@@ -30,8 +30,35 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展方法,获得枚举的Description
|
||||
/// </summary>
|
||||
/// <param name="value">枚举值</param>
|
||||
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
|
||||
/// <returns>枚举的Description</returns>
|
||||
public static string GetDescription(this Enum value, Boolean nameInstead = true)
|
||||
{
|
||||
Type type = value.GetType();
|
||||
string name = Enum.GetName(type, value);
|
||||
if (name == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user