using System;
using System.ComponentModel;
using System.Reflection;
namespace learun.util
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.09.26
/// 描 述:获取实体类Attribute自定义属性
///
public class EnumAttribute
{
///
/// 返回枚举项的描述信息。
///
/// 要获取描述信息的枚举项。
/// 枚举想的描述信息。
public static string GetDescription(Enum value)
{
Type enumType = value.GetType();
// 获取枚举常数名称。
string name = Enum.GetName(enumType, value);
if (name != null)
{
// 获取枚举字段。
FieldInfo fieldInfo = enumType.GetField(name);
if (fieldInfo != null)
{
// 获取描述的属性。
if (Attribute.GetCustomAttribute(fieldInfo,
typeof(DescriptionAttribute), false) is DescriptionAttribute attr)
{
return attr.Description;
}
}
}
return null;
}
}
}