using MAX.Core.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace MAX.Core.Helpers;
///
/// 枚举操作
///
public static partial class EnumHelper
{
///
/// 获取实例
///
/// 枚举类型
/// 成员名或值,范例:Enum1枚举有成员A=0,则传入"A"或"0"获取 Enum1.A
public static TEnum Parse(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(), 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(string description)
{
return (TEnum)ParseByDescription(Common.GetType(), description);
}
public static object ParseByDescription(Type enumObj, string description)
{
List listitem = GetItems(enumObj);
foreach (EnumItem item in listitem)
{
if (item.Description == description)
{
return Enum.Parse(enumObj, item.Numeric.ToString(), true);
}
}
return null;
}
///
/// 获取成员名
///
/// 枚举类型
/// 成员名、值、实例均可,范例:Enum1枚举有成员A=0,则传入Enum1.A或0,获取成员名"A"
public static string GetName(object member)
{
return GetName(Common.GetType(), member);
}
///
/// 获取成员名
///
/// 枚举类型
/// 成员名、值、实例均可
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);
}
///
/// 获取成员值
///
/// 枚举类型
/// 成员名、值、实例均可,范例:Enum1枚举有成员A=0,可传入"A"、0、Enum1.A,获取值0
public static int GetValue(object member)
{
return GetValue(Common.GetType(), member);
}
///
/// 获取成员值
///
/// 枚举类型
/// 成员名、值、实例均可
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);
}
///
/// 获取描述,使用System.ComponentModel.Description特性设置描述
///
/// 枚举类型
/// 成员名、值、实例均可
public static string GetDescription(object member)
{
return ReflectionHelper.GetDescription(GetName(member));
}
///
/// 获取描述,使用System.ComponentModel.Description特性设置描述
///
/// 枚举类型
/// 成员名、值、实例均可
public static string GetDescription(Type type, object member)
{
return ReflectionHelper.GetDescription(type, GetName(type, member));
}
///
/// 获取项集合,文本设置为Description,值为Value
///
/// 枚举类型
public static List GetItems()
{
return GetItems(typeof(TEnum));
}
///
/// 获取项集合,文本设置为Description,值为Value
///
/// 枚举类型
public static List GetItems(Type type)
{
type = Common.GetType(type);
if (type.IsEnum == false)
{
throw new InvalidOperationException($"类型 {type} 不是枚举");
}
List 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;
}
///
/// 添加描述项
///
private static void AddItem(Type type, ICollection 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));
}
///
/// 获取名称集合
///
/// 枚举类型
public static List GetNames()
{
return GetNames(typeof(TEnum));
}
///
/// 获取名称集合
///
/// 枚举类型
public static List GetNames(Type type)
{
type = Common.GetType(type);
if (type.IsEnum == false)
{
throw new InvalidOperationException($"类型 {type} 不是枚举");
}
List result = [];
foreach (FieldInfo field in type.GetFields())
{
if (!field.FieldType.IsEnum)
{
continue;
}
result.Add(field.Name);
}
return result;
}
///
/// 获取名称集合
///
/// 枚举类型
public static List GetDescriptions()
{
return GetDescriptions(typeof(TEnum));
}
///
/// 获取名称集合
///
/// 枚举类型
public static List GetDescriptions(Type type)
{
type = Common.GetType(type);
if (type.IsEnum == false)
{
throw new InvalidOperationException($"类型 {type} 不是枚举");
}
List result = [];
foreach (FieldInfo field in type.GetFields())
{
if (!field.FieldType.IsEnum)
{
continue;
}
string description = ReflectionHelper.GetDescription(field);
result.Add(description);
}
return result;
}
///
/// 是否单一标记
///
///
///
public static bool IsSingleFlag(Enum obj)
{
int intValue = GetValue(obj.GetType(), obj);
return (intValue > 0) && ((intValue & (intValue - 1)) == 0);
}
///
/// 获取第一个标记
///
///
///
///
public static T GetFirstFlag(T value) where T : Enum
{
long v = System.Convert.ToInt64(value);
long first = v & -v;
return Enum.GetValues(typeof(T)).Cast().First(flag => System.Convert.ToInt64(flag) == first);
}
}
///
/// Enum展示类型
///
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 : 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();
return attr?.Description ?? string.Empty;
}
}