115 lines
4.0 KiB
C#
115 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace NaiBa.Util.Extension
|
|
{
|
|
public static partial class Extensions
|
|
{
|
|
#region 枚举成员转成dictionary类型
|
|
/// <summary>
|
|
/// 转成dictionary类型
|
|
/// </summary>
|
|
/// <param name="enumType"></param>
|
|
/// <returns></returns>
|
|
public static Dictionary<int, string> EnumToDictionary(this Type enumType)
|
|
{
|
|
Dictionary<int, string> dictionary = new Dictionary<int, string>();
|
|
Type typeDescription = typeof(DescriptionAttribute);
|
|
FieldInfo[] fields = enumType.GetFields();
|
|
int sValue = 0;
|
|
string sText = string.Empty;
|
|
foreach (FieldInfo field in fields)
|
|
{
|
|
if (field.FieldType.IsEnum)
|
|
{
|
|
sValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
|
|
object[] arr = field.GetCustomAttributes(typeDescription, true);
|
|
if (arr.Length > 0)
|
|
{
|
|
DescriptionAttribute da = (DescriptionAttribute)arr[0];
|
|
sText = da.Description;
|
|
}
|
|
else
|
|
{
|
|
sText = field.Name;
|
|
}
|
|
dictionary.Add(sValue, sText);
|
|
}
|
|
}
|
|
return dictionary.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 枚举成员转成键值对Json字符串
|
|
/// </summary>
|
|
/// <param name="enumType"></param>
|
|
/// <returns></returns>
|
|
public static string EnumToDictionaryString(this Type enumType)
|
|
{
|
|
List<KeyValuePair<int, string>> dictionaryList = EnumToDictionary(enumType).ToList();
|
|
var sJson = JsonConvert.SerializeObject(dictionaryList);
|
|
return sJson;
|
|
}
|
|
#endregion
|
|
|
|
#region 根据类型获取枚举的描述
|
|
/// <summary>
|
|
/// 获取枚举值对应的描述
|
|
/// </summary>
|
|
/// <param name="enumType"></param>
|
|
/// <returns></returns>
|
|
public static string GetDescription(this System.Enum enumType)
|
|
{
|
|
FieldInfo EnumInfo = enumType.GetType().GetField(enumType.ToString());
|
|
if (EnumInfo != null)
|
|
{
|
|
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
if (EnumAttributes.Length > 0)
|
|
{
|
|
return EnumAttributes[0].Description;
|
|
}
|
|
}
|
|
return enumType.ToString();
|
|
}
|
|
#endregion
|
|
|
|
#region 根据值获取枚举的描述
|
|
public static string GetDescriptionByEnum<T>(this object obj)
|
|
{
|
|
var tEnum = System.Enum.Parse(typeof(T), obj.ParseToString()) as System.Enum;
|
|
var description = tEnum.GetDescription();
|
|
return description;
|
|
}
|
|
#endregion
|
|
|
|
#region 根据描述获取枚举值
|
|
public static int GetEnumValueByDescription<T>(this string description)
|
|
{
|
|
Dictionary<int, string> dict = typeof(T).EnumToDictionary();
|
|
int value = dict.Where(p => p.Value == description).Select(p => p.Key).FirstOrDefault();
|
|
return value;
|
|
}
|
|
#endregion
|
|
|
|
#region 根据枚举名称获取值
|
|
public static int GetEnumValueByName<T>(this string name)
|
|
{
|
|
return (int)System.Enum.Parse(typeof(T), name);
|
|
}
|
|
#endregion
|
|
|
|
#region 根据枚举值获取名称
|
|
public static object GetEnumNameByValue<T>(this int value)
|
|
{
|
|
return System.Enum.ToObject(typeof(T), value);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|