/// <summary>
/// 获取枚举的描述
/// </summary>
/// <param name="value">枚举值</param>
/// <returns></returns>
public static string GetDesc(this Enum value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (!string.IsNullOrWhiteSpace(name))
{
FieldInfo field = type.GetField(name);
if (field != null && (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attr))
{
return attr.Description;
}
}
return value.ToString();
}
//定义的枚举
public enum LxEnum
{
[Description("办件类型")]
BJLX=1,
[Description("办件分组")]
BJFZ=2,
}
调用示例:
//@param 您的枚举字符串值
string desc=string.Empty;//描述名称
if(Enum.TryParse(@param,out LxEnum enumValue))
{
desc=enumValue.GetDesc();
}