using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Elight.Utility.Enum { /// /// /// /// Author:mxg /// CreatedTimed:2022-05-13 06:00 PM public static class EnumExtension { public static string? ToDescription(this System.Enum item) { string? name = item.ToString(); var desc = item.GetType().GetField(name)?.GetCustomAttribute(); return desc?.Description ?? name; } public static long ToInt64(this System.Enum item) { return Convert.ToInt64(item); } public static List> ToList(this System.Enum value, bool ignoreNull = false) { var enumType = value.GetType(); if (!enumType.IsEnum) return null; return System.Enum.GetValues(enumType).Cast() .Where(m => !ignoreNull || !m.ToString().Equals("Null")).Select(x => new Dictionary { ["Label"] = x.ToDescription(), ["Value"] = x }).ToList(); } public static List> ToList(bool ignoreNull = false) { var enumType = typeof(T); if (!enumType.IsEnum) return null; return System.Enum.GetValues(enumType).Cast() .Where(m => !ignoreNull || !m.ToString().Equals("Null")).Select(x => new Dictionary { ["Label"] = x.ToDescription(), ["Value"] = x }).ToList(); } } }