c# EnumUtil
public class EnumUtil { /// <summary> /// convert obj to Enum /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static T Obj2Enum<T>(object obj) { if (obj != null) { if (!string.IsNullOrEmpty(obj.ToString().Trim())) { try { T t = (T)Enum.Parse(typeof(T), obj.ToString().Trim()); return t; } catch (Exception ex) { } } } return default(T); } /// <summary> /// Convert Enum to DataTable /// /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static DataTable Enum2Dt<T>() { Type t = typeof(T); DataTable dt = new DataTable(); dt.Columns.Add("EnumValue"); dt.Columns.Add("EnumText"); foreach (int obj in Enum.GetValues(t)) { DataRow dr = dt.NewRow(); dr["EnumValue"] = obj.ToString(); dr["EnumText"] = Enum.GetName(t, obj); dt.Rows.Add(dr); } return dt; } /// <summary> /// get enum value /// </summary> /// <param name="v"></param> /// <returns></returns> public static int GetEnumValue(Enum v) { return v.GetHashCode(); } public static string GetEnumString<T>(object obj) { if (obj==null) { return null; } T t = (T)Enum.Parse(typeof(T), obj.ToString().Trim()); return t.ToString(); } /// <summary> /// zlh 20111119 /// get enum name /// </summary> /// <param name="v"></param> /// <returns></returns> public static string GetEnumString(Enum v) { return v.ToString(); } /// <summary> /// convert enum to list /// </summary> /// <param name="type"></param> /// <returns></returns> public static IList<EnumEntity> Enum2List<T>() { IList<EnumEntity> list = new List<EnumEntity>(); foreach (int obj in Enum.GetValues(typeof(T))) { EnumEntity item = new EnumEntity(); item.EnumValue= obj; ; item.EnumText = Enum.GetName(typeof(T), obj); list.Add(item); } return list; } } /// <summary> /// key value /// </summary> public class EnumEntity { public string EnumText { get; set; } public int EnumValue { get; set; } }