You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.1 KiB
43 lines
1.1 KiB
using System;
|
|
using System.ComponentModel;
|
|
using System.Reflection;
|
|
|
|
namespace learun.util
|
|
{
|
|
/// <summary>
|
|
/// 版 本 EasyCode EC管理后台
|
|
/// Copyright (c) 2019-present EC管理有限公司
|
|
/// 创建人:tobin
|
|
/// 日 期:2019.09.26
|
|
/// 描 述:获取实体类Attribute自定义属性
|
|
/// </summary>
|
|
public class EnumAttribute
|
|
{
|
|
/// <summary>
|
|
/// 返回枚举项的描述信息。
|
|
/// </summary>
|
|
/// <param name="value">要获取描述信息的枚举项。</param>
|
|
/// <returns>枚举想的描述信息。</returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|