public static class EnumHelper
{
#region get
/// <summary>
/// 獲得枚舉類型所包含的全部項的清單
/// </summary>
/// <param name="enumType">枚舉的類型</param>
/// <returns></returns>
public static List<EnumItem> GetEnumItems(Type enumType)
{
return GetEnumItems(enumType, false);
}
/// <summary>
/// 獲得枚舉類型所包含的全部項的清單,包含"All"。
/// </summary>
/// <param name="enumType">枚舉對象類型</param>
/// <returns></returns>
public static List<EnumItem> GetEnumItemsWithAll(Type enumType)
{
return GetEnumItems(enumType, true);
}
/// <summary>
/// 獲得枚舉類型所包含的全部項的清單
/// </summary>
/// <param name="enumType">枚舉對象類型</param>
/// <param name="withAll">是否需要包含'All'</param>
/// <returns></returns>
public static List<EnumItem> GetEnumItems(Type enumType, bool withAll)
{
List<EnumItem> list = new List<EnumItem>();
if (enumType.IsEnum != true)
{
//whether the type is enum type
throw new InvalidOperationException();
}
if (withAll == true)
list.Add(new EnumItem(-1, "All"));
// 獲得特性Description的類型資訊
Type typeDescription = typeof(DescriptionAttribute);
// 獲得枚舉的字段資訊(因為枚舉的值實際上是一個static的字段的值)
System.Reflection.FieldInfo[] fields = enumType.GetFields();
// 檢索所有字段
foreach (FieldInfo field in fields)
{
// 過濾掉一個不是枚舉值的,記錄的是枚舉的源類型
if (field.FieldType.IsEnum == false)
continue;
// 通過字段的名字得到枚舉的值
int value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
string text = string.Empty;
// 獲得這個字段的所有自定義特性,這裡隻查找Description特性
object[] arr = field.GetCustomAttributes(typeDescription, true);
if (arr.Length > 0)
{
// 因為Description自定義特性不允許重複,是以隻取第一個
DescriptionAttribute aa = (DescriptionAttribute)arr[0];
// 獲得特性的描述值
text = aa.Description;
}
else
{
// 如果沒有特性描述,那麼就顯示英文的字段名
text = field.Name;
}
list.Add(new EnumItem(value, text));
}
return list;
}
/// <summary>
/// the the enum value's descrption attribute information
/// </summary>
/// <param name="enumType">the type of the enum</param>
/// <param name="value">the enum value</param>
/// <returns></returns>
public static string GetDescriptionByEnum<T>(T t)
{
if (t == null)
{
return null;
}
Type enumType = typeof(T);
List<EnumItem> list = GetEnumItems(enumType);
foreach (EnumItem item in list)
{
if (Convert.ToInt32(item.Key) == Convert.ToInt32(t))
return item.Value.ToString();
}
return string.Empty;
}
public static string GetDescriptionByEnum(object t)
{
if (t == null)
{
return string.Empty;
}
Type enumType = t.GetType();
List<EnumItem> list = GetEnumItems(enumType);
foreach (EnumItem item in list)
{
if (Convert.ToInt32(item.Key) == Convert.ToInt32(t))
return item.Value.ToString();
}
return string.Empty;
}
/// <summary>
/// get the enum value's int mode value
/// </summary>
/// <param name="enumType">the type of the enum</param>
/// <param name="value">the enum value's descrption</param>
/// <returns></returns>
public static int GetValueByDescription<T>(string description)
{
Type enumType = typeof(T);
List<EnumItem> list = GetEnumItems(enumType);
foreach (EnumItem item in list)
{
if (item.Value.ToString().ToLower() == description.Trim().ToLower())
return Convert.ToInt32(item.Key);
}
return -1;
}
/// <summary>
/// get the Enum value according to the its decription
/// </summary>
/// <param name="enumType">the type of the enum</param>
/// <param name="value">the description of the EnumValue</param>
/// <returns></returns>
public static T GetEnumByDescription<T>(string description)
{
if (description == null)
{
return default(T);
}
Type enumType = typeof(T);
List<EnumItem> list = GetEnumItems(enumType);
foreach (EnumItem item in list)
{
if (item.Value.ToString().ToLower() == description.Trim().ToLower())
return (T)item.Key;
}
return default(T);
}
/// <summary>
/// get the description attribute of a Enum value
/// </summary>
/// <param name="enumType">the type of the enum</param>
/// <param name="value">enum value name</param>
/// <returns></returns>
public static T GetEnumByName<T>(string name)
{
Type enumType = typeof(T);
List<EnumItem> list = GetEnumItems(enumType);
bool flag = false;
foreach (EnumItem item in list)
{
if (item.Value.ToString().ToLower() == name.Trim().ToLower())
{
flag = true;
return (T)item.Key;
}
}
if (!flag)
{
throw new ArgumentException("Can not found specify the name of the enum", "name");
}
return default(T);
}
public static T GetEnumByValue<T>(object value)
{
bool flag = false;
if (value == null)
throw new ArgumentNullException("value");
try
{
Type enumType = typeof(T);
List<EnumItem> list = GetEnumItems(enumType);
foreach (EnumItem item in list)
{
if (item.Key.ToString().Trim().ToLower() == value.ToString().Trim().ToLower())
{
flag = true;
return (T)item.Key;
}
}
if (!flag)
{
throw new ArgumentException("Can not found specify the value of the enum", "value");
}
return default(T);
}
catch
{
return default(T);
}
}
public static int? GetValueByEnum(object value)
{
if (value == null)
return null;
try
{
return (int)value;
}
catch
{
return null;
}
}
#endregion
#region Parse Enum
/// <summary>
/// 提供Value的字元,轉換為對應的枚舉對象
/// <remarks>适用于枚舉對象值定義為Char類型的</remarks>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="c"></param>
/// <returns></returns>
public static T Parse<T>(char c) where T : struct
{
return Parse<T>((ulong)c);
}
/// <summary>
/// 提供Value的字元,轉換為對應的枚舉對象
/// <remarks>适用于枚舉對象值定義為Int類型的</remarks>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="l"></param>
/// <returns></returns>
public static T Parse<T>(ulong l) where T : struct
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Need System.Enum as generic type!");
}
object o = Enum.Parse(typeof(T), l.ToString());
if (Enum.IsDefined(typeof(T), o))
{
return (T)o;
}
throw new InvalidCastException();
}
/// <summary>
/// 提供Value的字元,轉換為對應的枚舉對象
/// <remarks>适用于枚舉對象值定義為Char類型的</remarks>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T Parse<T>(string value) where T : struct
{
if (value == null || value.Trim().Length != 1)
{
throw new ArgumentException("Invalid cast,value must be one character!");
}
return Parse<T>(value[0]);
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="c"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryParse<T>(char c, out T result) where T : struct
{
return TryParse<T>((ulong)c, out result);
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryParse<T>(string value, out T result) where T : struct
{
try
{
if (value == null || value.Trim().Length != 1)
{
throw new ArgumentException("Invalid cast,value must be one character!");
}
return TryParse<T>(value[0], out result);
}
catch
{
result = default(T);
return false;
}
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryParse<T>(ulong value, out T result) where T : struct
{
try
{
result = Parse<T>(value);
return true;
}
catch
{
result = default(T);
return false;
}
}
#endregion
}
public class EnumItem
{
private object m_key;
private object m_value;
public object Key
{
get { return m_key; }
set { m_key = value; }
}
public object Value
{
get { return m_value; }
set { m_value = value; }
}
public EnumItem(object _key, object _value)
{
m_key = _key;
m_value = _value;
}
}
如果您覺得本文對你有用,不妨幫忙點個贊,或者在評論裡給我一句贊美,小小成就都是今後繼續為大家編寫優質文章的動力!
歡迎您持續關注我的部落格:)
作者:Ken Wang
出處:http://www.cnblogs.com/Wolfmanlq/
版權所有,歡迎保留原文連結進行轉載:)