枚舉類型是定義了一組“符號名稱/值”配對。枚舉類型是強類型的。每個枚舉類型都是從system.Enum派生,又從system.ValueType派生,而system.ValueType又從system.Object派生,所以枚舉類型是指類型。
編譯枚舉類型時,C#編譯器會把每個符號轉換成類型的一個常量字段。C#編譯器將枚舉類型視為基元類型。
1.獲取枚舉列表:
/// <summary> /// 獲取枚舉列表 /// </summary> /// <param name="enumType">枚舉的類型</param> /// <returns>枚舉列表</returns> public static Dictionary<int, string> GetEnumList(Type enumType) { var dic = new Dictionary<int, string>(); try { var fd = enumType.GetFields(); for (var index = 1; index < fd.Length; ++index) { var info = fd[index]; var fieldValue = System.Enum.Parse(enumType, fd[index].Name); var attrs = info.GetCustomAttributes(typeof(EnumTextAttribute), false); foreach (EnumTextAttribute attr in attrs) { var key = (int)fieldValue; if (key == -100) continue; var value = attr.Text; dic.Add(key, value); } } return dic; } catch (Exception ex) { throw new Exception(ex.Message); } }
2.獲取枚舉名稱:
/// <summary> /// 獲取枚舉名稱 /// </summary> /// <param name="enumType">枚舉的類型</param> /// <param name="id">枚舉值</param> /// <returns>如果枚舉值存在,返回對應的枚舉名稱,否則,返回空字符</returns> public static string GetEnumTextById(Type enumType, int id) { var ret = string.Empty; try { var dic = GetEnumList(enumType); foreach (var item in dic) { if (item.Key != id) continue; ret = item.Value; break; } return ret; } catch (Exception ex) { throw new Exception(ex.Message); } }
3.根據枚舉值獲取對應中文描述:
/// <summary> /// 根據枚舉值獲取對應中文描述 /// </summary> /// <param name="enumValue">枚舉值</param> /// <returns>枚舉值中文描述</returns> public static string GetEnumTextByEnum(object enumValue) { var ret = string.Empty; if ((int)enumValue == -1) return ret; try { var dic = GetEnumList(enumValue.GetType()); foreach (var item in dic) { if (item.Key != (int)enumValue) continue; ret = item.Value; break; } return ret; } catch (Exception ex) { throw new Exception(ex.Message); } }
4.獲取枚舉名稱:
/// <summary> /// 獲取枚舉名稱 /// </summary> /// <param name="enumType">枚舉的類型</param> /// <param name="index">枚舉值的位置編號</param> /// <returns>如果枚舉值存在,返回對應的枚舉名稱,否則,返回空字符</returns> public static string GetEnumTextByIndex(Type enumType, int index) { var ret = string.Empty; var dic = GetEnumList(enumType); if (index < 0 || index > dic.Count) return ret; var i = 0; foreach (var item in dic) { if (i == index) { ret = item.Value; break; } i++; } return ret; }
5.獲取枚舉值:
/// <summary> /// 獲取枚舉值 /// </summary> /// <param name="enumType">枚舉的類型</param> /// <param name="name">枚舉名稱</param> /// <returns>如果枚舉名稱存在,返回對應的枚舉值,否則,返回-1</returns> public static int GetEnumIdByName(Type enumType, string name) { var ret = -1; if (string.IsNullOrEmpty(name)) return ret; var dic = GetEnumList(enumType); foreach (var item in dic) { if (string.Compare(item.Value, name, StringComparison.Ordinal) != 0) continue; ret = item.Key; break; } return ret; }
6.獲取名字對應枚舉值:
/// <summary> /// 獲取名字對應枚舉值 /// </summary> /// <typeparam name="T">枚舉類型</typeparam> /// <param name="name">枚舉名稱</param> /// <returns></returns> public static T GetEnumIdByName<T>(string name) where T : new() { var type = typeof(T); var enumItem = (T)TypeDescriptor.GetConverter(type).ConvertFrom("-1"); if (string.IsNullOrEmpty(name)) return enumItem; try { var fd = typeof(T).GetFields(); for (var index = 1; index < fd.Length; ++index) { var info = fd[index]; var fieldValue = System.Enum.Parse(type, fd[index].Name); var attrs = info.GetCustomAttributes(typeof(EnumTextAttribute), false); if (attrs.Length != 1) continue; var attr = (EnumTextAttribute)attrs[0]; if (!name.Equals(attr.Text)) continue; enumItem = (T)fieldValue; break; } return enumItem; } catch (Exception ex) { throw new Exception(ex.Message); } }
7.獲取枚舉值所在的位置編號:
/// <summary> /// 獲取枚舉值所在的位置編號 /// </summary> /// <param name="enumType">枚舉的類型</param> /// <param name="name">枚舉名稱</param> /// <returns>如果枚舉名稱存在,返回對應的枚舉值的位置編號,否則,返回-1</returns> public static int GetEnumIndexByName(Type enumType, string name) { var ret = -1; if (string.IsNullOrEmpty(name)) return ret; var dic = GetEnumList(enumType); var i = 0; foreach (var item in dic) { if (string.Compare(item.Value, name, StringComparison.Ordinal) == 0) { ret = i; break; } i++; } return ret; }