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.

35 lines
972 B

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));
}
public static string GetNameByEnum<T>(Enum value) where T : Enum
{
return Enum.GetName(typeof(T), value);
}
}