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.
30 lines
843 B
30 lines
843 B
6 months ago
|
using System.ComponentModel;
|
||
|
using System.Reflection;
|
||
|
|
||
|
namespace Entity.Constant;
|
||
|
|
||
|
public class BaseEnumExtensions
|
||
|
{
|
||
|
//根据枚举获取注释
|
||
|
public static string GetDescription(Enum value)
|
||
|
{
|
||
|
FieldInfo field = value.GetType().GetField(value.ToString());
|
||
|
|
||
|
DescriptionAttribute attribute =
|
||
|
Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
|
||
|
|
||
|
return attribute == null ? value.ToString() : attribute.Description;
|
||
|
}
|
||
|
|
||
|
//根据code获取枚举
|
||
|
public static T GetEnumByCode<T>(int code) where T : Enum
|
||
|
{
|
||
|
return (T)Enum.ToObject(typeof(T), code);
|
||
|
}
|
||
|
|
||
|
//根据code获取值
|
||
|
public static string GetEnumDescriptionByCode<T>(int code) where T : Enum
|
||
|
{
|
||
|
return GetDescription((T)Enum.ToObject(typeof(T), code));
|
||
|
}
|
||
|
}
|