111 lines
3.7 KiB
C#
111 lines
3.7 KiB
C#
using System.Reflection;
|
|
|
|
namespace YLErp.Helpers
|
|
{
|
|
public static class EnumHelper
|
|
{
|
|
/// <summary>
|
|
/// get enum description by name
|
|
/// </summary>
|
|
/// <typeparam name="T">enum type</typeparam>
|
|
/// <param name="enumItemName">the enum name</param>
|
|
/// <returns></returns>
|
|
public static string GetDescriptionByName<TEnum>(TEnum enumItemName)
|
|
{
|
|
var fi = enumItemName.GetType().GetField(enumItemName.ToString());
|
|
|
|
if (fi == null)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
|
|
if (attributes != null && attributes.Length > 0)
|
|
{
|
|
return attributes[0].Description;
|
|
}
|
|
else
|
|
{
|
|
return enumItemName.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取枚举类型的键值对
|
|
/// <para>因为目前都是给页面生成代码用,为了使用方便,所以把Value类型改为了string</para>
|
|
/// </summary>
|
|
/// <param name="enumType"></param>
|
|
/// <returns></returns>
|
|
public static Dictionary<string, string> GetDescriptionsByEnum(Type enumType)
|
|
{
|
|
var resultList = new Dictionary<string, string>();
|
|
if (!enumType.IsEnum)
|
|
{
|
|
return resultList;
|
|
}
|
|
|
|
var fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
|
|
foreach (var fieldInfo in fields)
|
|
{
|
|
var value = (int)fieldInfo.GetValue(null);
|
|
var description = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
if (description == null || description.Length == 0)
|
|
{
|
|
resultList[fieldInfo.Name] = value.ToString();
|
|
}
|
|
else
|
|
{
|
|
resultList[description[0].Description] = value.ToString();
|
|
}
|
|
}
|
|
|
|
return resultList;
|
|
}
|
|
|
|
public static TEnum GetEnumByDescription<TEnum>(this string description) where TEnum : struct
|
|
{
|
|
var result = default(TEnum);
|
|
if (string.IsNullOrWhiteSpace(description))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var enumType = typeof(TEnum);
|
|
var fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
|
|
foreach (var fieldInfo in fields)
|
|
{
|
|
var des = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
if (des.Length > 0 && des[0].Description == description)
|
|
{
|
|
result = (TEnum)fieldInfo.GetValue(null);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static bool TryGetEnumByDescription<TEnum>(string description, out TEnum result) where TEnum : struct
|
|
{
|
|
result = default(TEnum);
|
|
if (string.IsNullOrWhiteSpace(description))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var enumType = typeof(TEnum);
|
|
var fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
|
|
foreach (var fieldInfo in fields)
|
|
{
|
|
var des = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
if (des.Length > 0 && des[0].Description == description)
|
|
{
|
|
result = (TEnum)fieldInfo.GetValue(null);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
} |