176 lines
4.0 KiB
C#
176 lines
4.0 KiB
C#
|
|
using System.ComponentModel;
|
|||
|
|
using System.ComponentModel.DataAnnotations;
|
|||
|
|
using System.Reflection;
|
|||
|
|
|
|||
|
|
namespace Sai.Toolkit.Core.Helpers;
|
|||
|
|
|
|||
|
|
public static class EnumHelpers
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取当前枚举的值和枚举描述
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<KeyValuePair<int, string>> GetAll<T>()
|
|||
|
|
where T : IComparable, IFormattable, IConvertible
|
|||
|
|
{
|
|||
|
|
var list = new List<KeyValuePair<int, string>>();
|
|||
|
|
foreach (var field in typeof(T).GetFields())
|
|||
|
|
{
|
|||
|
|
if (field.FieldType.IsEnum)
|
|||
|
|
{
|
|||
|
|
var tmp = field.GetValue(null);
|
|||
|
|
var enumValue = (Enum)tmp;
|
|||
|
|
var intValue = Convert.ToInt32(enumValue);
|
|||
|
|
var showName = enumValue.GetDescription();
|
|||
|
|
list.Add(new KeyValuePair<int, string>(intValue, showName));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return list;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取当前枚举值的描述,没有描述则返回空字符串
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="value"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string GetDescription(this Enum value)
|
|||
|
|
{
|
|||
|
|
var description = string.Empty;
|
|||
|
|
var fieldInfo = value.GetType().GetField(value.ToString());
|
|||
|
|
var attr = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
|
|||
|
|
if (attr != null)
|
|||
|
|
{
|
|||
|
|
description = attr.Description;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return description;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static Dictionary<string, T> GetDescriptionEnumMap<T>()
|
|||
|
|
where T : Enum
|
|||
|
|
{
|
|||
|
|
var results = new Dictionary<string, T>();
|
|||
|
|
|
|||
|
|
var values = Enum.GetValues(typeof(T));
|
|||
|
|
|
|||
|
|
foreach (T value in values)
|
|||
|
|
{
|
|||
|
|
var member = typeof(T).GetMember(value.ToString()).FirstOrDefault();
|
|||
|
|
|
|||
|
|
if (member is null)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var customAttributes = member.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|||
|
|
|
|||
|
|
var name = (customAttributes.FirstOrDefault() as DescriptionAttribute)?.Description;
|
|||
|
|
|
|||
|
|
if (name != null)
|
|||
|
|
{
|
|||
|
|
results.Add(name, value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return results;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static Dictionary<string, T> GetDisplayEnumDict<T>()
|
|||
|
|
where T : Enum
|
|||
|
|
{
|
|||
|
|
var results = new Dictionary<string, T>();
|
|||
|
|
|
|||
|
|
var values = Enum.GetValues(typeof(T));
|
|||
|
|
|
|||
|
|
foreach (T value in values)
|
|||
|
|
{
|
|||
|
|
var member = typeof(T).GetMember(value.ToString()).FirstOrDefault();
|
|||
|
|
|
|||
|
|
if (member is null)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var atts = member.GetCustomAttributes(typeof(DisplayAttribute), false);
|
|||
|
|
|
|||
|
|
var name = (atts.FirstOrDefault() as DisplayAttribute)?.Name;
|
|||
|
|
|
|||
|
|
if (name != null)
|
|||
|
|
{
|
|||
|
|
results.Add(name, value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return results;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取描述的扩展方法
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="enumObj"></param>
|
|||
|
|
/// <param name="value">枚举值</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string GetEnumDescription(this Enum enumObj, int? value)
|
|||
|
|
{
|
|||
|
|
var fields = enumObj.GetType().GetFields();
|
|||
|
|
for (int i = 1, count = fields.Length; i < count; i++)
|
|||
|
|
{
|
|||
|
|
if ((int)Enum.Parse(enumObj.GetType(), fields[i].Name) == value)
|
|||
|
|
{
|
|||
|
|
var enumAttributes = (DescriptionAttribute[])fields[i].GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|||
|
|
if (enumAttributes.Length > 0)
|
|||
|
|
{
|
|||
|
|
return enumAttributes[0].Description;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return string.Empty;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static void GetInfo(Type enumType)
|
|||
|
|
{
|
|||
|
|
_ = Enum.GetValues(enumType); //得到的是枚举类型,用于遍历枚举类
|
|||
|
|
|
|||
|
|
_ = Enum.GetNames(enumType); //得到的是枚举类的字符串
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static List<string> TraverseEnum<T>()
|
|||
|
|
{
|
|||
|
|
List<string> pName = new();
|
|||
|
|
foreach (T p in Enum.GetValues(typeof(T)))
|
|||
|
|
{
|
|||
|
|
pName.Add(p.ToString());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return pName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static bool TryParseByDescAttr<T>(string value, out T result)
|
|||
|
|
where T : Enum
|
|||
|
|
{
|
|||
|
|
if (value is null)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(value));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var descs = GetDescriptionEnumMap<T>();
|
|||
|
|
|
|||
|
|
return !descs.TryGetValue(value, out result) ? throw new InvalidEnumArgumentException() : true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static bool TryParseByDisplayAttr<T>(string value, out T result)
|
|||
|
|
where T : Enum
|
|||
|
|
{
|
|||
|
|
if (value is null)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(value));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var dict = GetDisplayEnumDict<T>();
|
|||
|
|
|
|||
|
|
return !dict.TryGetValue(value, out result) ? throw new InvalidEnumArgumentException() : true;
|
|||
|
|
}
|
|||
|
|
}
|