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.
61 lines
1.8 KiB
61 lines
1.8 KiB
2 years ago
|
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
|
||
|
{
|
||
|
/// <summary>
|
||
|
///
|
||
|
/// </summary>
|
||
|
/// 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<DescriptionAttribute>();
|
||
|
return desc?.Description ?? name;
|
||
|
}
|
||
|
|
||
|
public static long ToInt64(this System.Enum item)
|
||
|
{
|
||
|
return Convert.ToInt64(item);
|
||
|
}
|
||
|
|
||
|
public static List<Dictionary<string, object>> ToList(this System.Enum value, bool ignoreNull = false)
|
||
|
{
|
||
|
var enumType = value.GetType();
|
||
|
|
||
|
if (!enumType.IsEnum)
|
||
|
return null;
|
||
|
|
||
|
return System.Enum.GetValues(enumType).Cast<System.Enum>()
|
||
|
.Where(m => !ignoreNull || !m.ToString().Equals("Null")).Select(x => new Dictionary<string, object?>
|
||
|
{
|
||
|
["Label"] = x.ToDescription(),
|
||
|
["Value"] = x
|
||
|
}).ToList();
|
||
|
}
|
||
|
|
||
|
public static List<Dictionary<string, object>> ToList<T>(bool ignoreNull = false)
|
||
|
{
|
||
|
var enumType = typeof(T);
|
||
|
|
||
|
if (!enumType.IsEnum)
|
||
|
return null;
|
||
|
|
||
|
return System.Enum.GetValues(enumType).Cast<System.Enum>()
|
||
|
.Where(m => !ignoreNull || !m.ToString().Equals("Null")).Select(x => new Dictionary<string, object?>
|
||
|
{
|
||
|
["Label"] = x.ToDescription(),
|
||
|
["Value"] = x
|
||
|
}).ToList();
|
||
|
}
|
||
|
}
|
||
|
}
|