using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Reflection;
|
using SystemEnum = System.Enum;
|
|
namespace HxEnum
|
{
|
public static class EnumManagement
|
{
|
#region 获取枚举描述
|
/// <summary>
|
/// 获取枚举描述
|
/// </summary>
|
/// <param name="enumSubitem">枚举值</param>
|
/// <returns>返回枚举描述</returns>
|
public static string GetEnumDescription(object enumSubitem)
|
{
|
enumSubitem = (System.Enum)enumSubitem;
|
string strValue = enumSubitem.ToString();
|
|
FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
|
|
if (fieldinfo != null)
|
{
|
|
Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
if (objs == null || objs.Length == 0)
|
{
|
return strValue;
|
}
|
else
|
{
|
DescriptionAttribute da = (DescriptionAttribute)objs[0];
|
return da.Description;
|
}
|
}
|
else
|
{
|
return "不限";
|
}
|
|
}
|
#endregion
|
|
#region 根据枚举的int值获取枚举值的详细说明
|
/// <summary>
|
/// 根据枚举的int值获取枚举值的详细说明
|
/// </summary>
|
/// <typeparam name="T">枚举类型</typeparam>
|
/// <param name="currentValue">枚举的Int值</param>
|
/// <returns>枚举值说明</returns>
|
public static string GetFieldTextByValue<T>(int currentValue)
|
{
|
T currentType = GetField<T>(currentValue);
|
//return GetFieldText<T>(currentType);
|
return GetEnumDescription(currentType);
|
}
|
|
/// <summary>
|
/// 根据枚举的int值类型转换为枚举类型
|
/// </summary>
|
/// <typeparam name="T">枚举类型</typeparam>
|
/// <param name="currentValue">枚举的int值</param>
|
/// <returns>枚举类型</returns>
|
public static T GetField<T>(int currentValue)
|
{
|
return (T)SystemEnum.Parse(typeof(T), currentValue.ToString());
|
}
|
|
/// <summary>
|
/// 获取枚举值的说明
|
/// </summary>
|
/// <typeparam name="T">枚举类型</typeparam>
|
/// <param name="enumType">枚举类型值</param>
|
/// <returns>枚举值说明</returns>
|
public static string GetFieldText<T>(T enumType)
|
{
|
Type currentType = enumType.GetType();
|
//获取字段信息
|
FieldInfo[] fields = currentType.GetFields();
|
|
if (fields == null || fields.Length < 1)
|
{
|
|
return enumType.ToString();
|
}
|
|
for (int i = 0; i < fields.Length; i++)
|
{
|
FieldInfo field = fields[i];
|
|
//判断名称是否相等
|
if (field.Name != enumType.ToString())
|
{
|
continue;
|
}
|
|
//反射自定义属性
|
object[] attributes = field.GetCustomAttributes(
|
typeof(DescriptionAttribute), false);
|
|
foreach (Attribute attribute in attributes)
|
{
|
//类型转换找到一个Description,用Description作为成员名称
|
DescriptionAttribute currentAttribute =
|
attribute as DescriptionAttribute;
|
|
if (currentAttribute != null)
|
{
|
return currentAttribute.Description;
|
}
|
}
|
}
|
|
//如果没有检测到合适的注释,则用默认名称
|
return enumType.ToString();
|
|
}
|
#endregion
|
|
#region 获取枚举值int
|
/// <summary>
|
/// 获取枚举值int
|
/// </summary>
|
/// <param name="enumSubitem">枚举值</param>
|
/// <returns>返回枚举值</returns>
|
public static int GetEnumValue(object enumSubitem)
|
{
|
return (int)enumSubitem;
|
}
|
#endregion
|
|
#region 获取枚举所有值,变成字典
|
/// <summary>
|
/// 获取枚举所有值,变成字典(键:字母,值:中文)
|
/// </summary>
|
/// <typeparam name="T">枚举对象</typeparam>
|
/// <returns></returns>
|
public static Dictionary<string, string> EnumToDictionaryKey<T>()
|
{
|
Dictionary<string, string> dic = new Dictionary<string, string>();
|
if (!typeof(T).IsEnum)
|
{
|
return dic;
|
}
|
string desc = string.Empty;
|
foreach (var item in Enum.GetValues(typeof(T)))
|
{
|
var attrs = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
|
if (attrs != null && attrs.Length > 0)
|
{
|
DescriptionAttribute descAttr = attrs[0] as DescriptionAttribute;
|
desc = descAttr.Description;
|
}
|
dic.Add(item.ToString(), desc);
|
}
|
return dic;
|
}
|
|
/// <summary>
|
/// 获取枚举所有值,变成字典(键:数字,值:中文)
|
/// </summary>
|
/// <typeparam name="T">枚举对象</typeparam>
|
/// <returns></returns>
|
public static Dictionary<string, string> EnumToDictionaryValue<T>()
|
{
|
Dictionary<string, string> dic = new Dictionary<string, string>();
|
if (!typeof(T).IsEnum)
|
{
|
return dic;
|
}
|
string desc = string.Empty;
|
foreach (var item in Enum.GetValues(typeof(T)))
|
{
|
var attrs = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
|
if (attrs != null && attrs.Length > 0)
|
{
|
DescriptionAttribute descAttr = attrs[0] as DescriptionAttribute;
|
desc = descAttr.Description;
|
}
|
dic.Add(GetEnumValue(item).ToString(), desc);
|
}
|
return dic;
|
}
|
|
/// <summary>
|
/// 字符串转Enum
|
/// </summary>
|
/// <typeparam name="T">枚举</typeparam>
|
/// <param name="str">字符串</param>
|
/// <returns>转换的枚举</returns>
|
public static T ToEnum<T>(this string str)
|
{
|
return (T)Enum.Parse(typeof(T), str);
|
}
|
|
/// <summary>
|
/// 获取所有枚举
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <returns></returns>
|
public static List<object> GetALLEnum<T>()
|
{
|
List<object> list = new List<object>();
|
|
foreach (var item in Enum.GetValues(typeof(T)))
|
{
|
list.Add(item);
|
}
|
return list;
|
}
|
#endregion
|
}
|
}
|