using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
public static class EnumHelper<T>
{
public static string GetDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return en.ToString();
}
public static T Parse(string value)
{
return (T)Enum.Parse(typeof(T), value);
}
public static IList<T> GetValues()
{
IList<T> list = new List<T>();
foreach (object value in Enum.GetValues(typeof(T)))
{
list.Add((T)value);
}
return list;
}
public static Dictionary<T, string> GetValueDescriptionDictionary()
{
Dictionary<T, string> dictionary = new Dictionary<T, string>();
foreach (object value in Enum.GetValues(typeof(T)))
{
dictionary.Add((T)value, GetDescription((Enum)value));
}
return dictionary;
}
}
枚举操作类
最新推荐文章于 2022-01-06 15:35:41 发布