341 lines
8.1 KiB
C#
341 lines
8.1 KiB
C#
using MAX.Core.Extensions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace MAX.Core.Helpers;
|
|
|
|
/// <summary>
|
|
/// 枚举操作
|
|
/// </summary>
|
|
public static partial class EnumHelper
|
|
{
|
|
/// <summary>
|
|
/// 获取实例
|
|
/// </summary>
|
|
/// <typeparam name="TEnum">枚举类型</typeparam>
|
|
/// <param name="member">成员名或值,范例:Enum1枚举有成员A=0,则传入"A"或"0"获取 Enum1.A</param>
|
|
public static TEnum Parse<TEnum>(object member)
|
|
{
|
|
string value = member.SafeString();
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
if (typeof(TEnum).IsGenericType)
|
|
{
|
|
return default;
|
|
}
|
|
|
|
throw new ArgumentNullException(nameof(member));
|
|
}
|
|
return (TEnum)Enum.Parse(Common.GetType<TEnum>(), value, true);
|
|
}
|
|
public static object Parse(Type enumObj, object member)
|
|
{
|
|
string value = member.SafeString();
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
if (enumObj.IsGenericType)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
throw new ArgumentNullException(nameof(member));
|
|
}
|
|
return Enum.Parse(enumObj, value, true);
|
|
}
|
|
public static TEnum ParseByDescription<TEnum>(string description)
|
|
{
|
|
return (TEnum)ParseByDescription(Common.GetType<TEnum>(), description);
|
|
}
|
|
public static object ParseByDescription(Type enumObj, string description)
|
|
{
|
|
List<EnumItem> listitem = GetItems(enumObj);
|
|
foreach (EnumItem item in listitem)
|
|
{
|
|
if (item.Description == description)
|
|
{
|
|
return Enum.Parse(enumObj, item.Numeric.ToString(), true);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
/// <summary>
|
|
/// 获取成员名
|
|
/// </summary>
|
|
/// <typeparam name="TEnum">枚举类型</typeparam>
|
|
/// <param name="member">成员名、值、实例均可,范例:Enum1枚举有成员A=0,则传入Enum1.A或0,获取成员名"A"</param>
|
|
public static string GetName<TEnum>(object member)
|
|
{
|
|
return GetName(Common.GetType<TEnum>(), member);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取成员名
|
|
/// </summary>
|
|
/// <param name="type">枚举类型</param>
|
|
/// <param name="member">成员名、值、实例均可</param>
|
|
public static string GetName(Type type, object member)
|
|
{
|
|
if (type == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
if (member == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
if (member is string)
|
|
{
|
|
return member.ToString();
|
|
}
|
|
|
|
if (type.GetTypeInfo().IsEnum == false)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
return Enum.GetName(type, member);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取成员值
|
|
/// </summary>
|
|
/// <typeparam name="TEnum">枚举类型</typeparam>
|
|
/// <param name="member">成员名、值、实例均可,范例:Enum1枚举有成员A=0,可传入"A"、0、Enum1.A,获取值0</param>
|
|
public static int GetValue<TEnum>(object member)
|
|
{
|
|
return GetValue(Common.GetType<TEnum>(), member);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取成员值
|
|
/// </summary>
|
|
/// <param name="type">枚举类型</param>
|
|
/// <param name="member">成员名、值、实例均可</param>
|
|
public static int GetValue(Type type, object member)
|
|
{
|
|
string value = member.SafeString();
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
throw new ArgumentNullException(nameof(member));
|
|
}
|
|
|
|
return (int)Enum.Parse(type, member.ToString(), true);
|
|
}
|
|
/// <summary>
|
|
/// 获取描述,使用System.ComponentModel.Description特性设置描述
|
|
/// </summary>
|
|
/// <typeparam name="TEnum">枚举类型</typeparam>
|
|
/// <param name="member">成员名、值、实例均可</param>
|
|
public static string GetDescription<TEnum>(object member)
|
|
{
|
|
return ReflectionHelper.GetDescription<TEnum>(GetName<TEnum>(member));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取描述,使用System.ComponentModel.Description特性设置描述
|
|
/// </summary>
|
|
/// <param name="type">枚举类型</param>
|
|
/// <param name="member">成员名、值、实例均可</param>
|
|
public static string GetDescription(Type type, object member)
|
|
{
|
|
return ReflectionHelper.GetDescription(type, GetName(type, member));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取项集合,文本设置为Description,值为Value
|
|
/// </summary>
|
|
/// <typeparam name="TEnum">枚举类型</typeparam>
|
|
public static List<EnumItem> GetItems<TEnum>()
|
|
{
|
|
return GetItems(typeof(TEnum));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取项集合,文本设置为Description,值为Value
|
|
/// </summary>
|
|
/// <param name="type">枚举类型</param>
|
|
public static List<EnumItem> GetItems(Type type)
|
|
{
|
|
type = Common.GetType(type);
|
|
if (type.IsEnum == false)
|
|
{
|
|
throw new InvalidOperationException($"类型 {type} 不是枚举");
|
|
}
|
|
|
|
List<EnumItem> result = [];
|
|
foreach (FieldInfo field in type.GetFields())
|
|
{
|
|
AddItem(type, result, field);
|
|
}
|
|
|
|
return result.OrderBy(t => t.Numeric).ToList();
|
|
}
|
|
|
|
public static CommonItem GetItem(object obj)
|
|
{
|
|
CommonItem item = null;
|
|
if (obj is Enum em)
|
|
{
|
|
//item.Text = em.get
|
|
string text = em.ToString();
|
|
int value = GetValue(obj.GetType(), obj);
|
|
string description = em.ToDescription();
|
|
item = new CommonItem(text, value, des: description);
|
|
}
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加描述项
|
|
/// </summary>
|
|
private static void AddItem(Type type, ICollection<EnumItem> result, FieldInfo field)
|
|
{
|
|
if (!field.FieldType.IsEnum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string text = field.Name;
|
|
int value = GetValue(type, field.Name);
|
|
string description = ReflectionHelper.GetDescription(field);
|
|
result.Add(new EnumItem(text, value, description));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取名称集合
|
|
/// </summary>
|
|
/// <typeparam name="TEnum">枚举类型</typeparam>
|
|
public static List<string> GetNames<TEnum>()
|
|
{
|
|
return GetNames(typeof(TEnum));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取名称集合
|
|
/// </summary>
|
|
/// <param name="type">枚举类型</param>
|
|
public static List<string> GetNames(Type type)
|
|
{
|
|
type = Common.GetType(type);
|
|
if (type.IsEnum == false)
|
|
{
|
|
throw new InvalidOperationException($"类型 {type} 不是枚举");
|
|
}
|
|
|
|
List<string> result = [];
|
|
foreach (FieldInfo field in type.GetFields())
|
|
{
|
|
if (!field.FieldType.IsEnum)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
result.Add(field.Name);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取名称集合
|
|
/// </summary>
|
|
/// <typeparam name="TEnum">枚举类型</typeparam>
|
|
public static List<string> GetDescriptions<TEnum>()
|
|
{
|
|
return GetDescriptions(typeof(TEnum));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取名称集合
|
|
/// </summary>
|
|
/// <param name="type">枚举类型</param>
|
|
public static List<string> GetDescriptions(Type type)
|
|
{
|
|
type = Common.GetType(type);
|
|
if (type.IsEnum == false)
|
|
{
|
|
throw new InvalidOperationException($"类型 {type} 不是枚举");
|
|
}
|
|
|
|
List<string> result = [];
|
|
foreach (FieldInfo field in type.GetFields())
|
|
{
|
|
if (!field.FieldType.IsEnum)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string description = ReflectionHelper.GetDescription(field);
|
|
result.Add(description);
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 是否单一标记
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static bool IsSingleFlag(Enum obj)
|
|
{
|
|
int intValue = GetValue(obj.GetType(), obj);
|
|
return (intValue > 0) && ((intValue & (intValue - 1)) == 0);
|
|
}
|
|
/// <summary>
|
|
/// 获取第一个标记
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static T GetFirstFlag<T>(T value) where T : Enum
|
|
{
|
|
long v = System.Convert.ToInt64(value);
|
|
long first = v & -v;
|
|
return Enum.GetValues(typeof(T)).Cast<T>().First(flag => System.Convert.ToInt64(flag) == first);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Enum展示类型
|
|
/// </summary>
|
|
public enum EnumDisplayType
|
|
{
|
|
Text,
|
|
Value,
|
|
Description,
|
|
CommonItem
|
|
}
|
|
public class EnumItem
|
|
{
|
|
public string Name { get; init; }
|
|
public int Numeric { get; init; }
|
|
public string Description { get; init; }
|
|
|
|
public EnumItem(string name, int numeric, string description)
|
|
{
|
|
Name = name;
|
|
Numeric = numeric;
|
|
Description = description;
|
|
}
|
|
}
|
|
public class EnumItem<TEnum> : EnumItem where TEnum : struct, Enum
|
|
{
|
|
public TEnum Value { get; }
|
|
|
|
public EnumItem(TEnum value)
|
|
: base(value.ToString(), System.Convert.ToInt32(value), GetDescription(value))
|
|
{
|
|
Value = value;
|
|
}
|
|
private static string GetDescription(TEnum value)
|
|
{
|
|
var field = typeof(TEnum).GetField(value.ToString())!;
|
|
var attr = field.GetCustomAttribute<DescriptionAttribute>();
|
|
return attr?.Description ?? string.Empty;
|
|
}
|
|
|
|
} |