首先定義枚舉類型,以下:this
/// <summary> /// 板塊 /// </summary> public enum Plate { [Description("全部市場")] All = 0, [Description("滬深300")] HS300 = 1, [Description("創業板")] CYB = 2, [Description("上證50")] SZ50 = 3, [Description("中小板")] ZXB = 4, [Description("中證500")] ZZ500 = 5, [Description("包括指數")] BKZS = 6, }
接下來是Helper類spa
public static class EnumHelper { /// <summary> /// 獲取枚舉值的Description /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns></returns> public static string GetDescription<T>(this T value) where T : struct { string result = value.ToString(); Type type = typeof(T); FieldInfo info = type.GetField(value.ToString()); var attributes = info.GetCustomAttributes(typeof(DescriptionAttribute), true); if (attributes != null && attributes.FirstOrDefault() != null) { result = (attributes.First() as DescriptionAttribute).Description; } return result; } /// <summary> /// 根據Description獲取枚舉值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns></returns> public static T GetValueByDescription<T>(this string description) where T : struct { Type type = typeof(T); foreach (var field in type.GetFields()) { if (field.Name == description) { return (T)field.GetValue(null); } var attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), true); if (attributes != null && attributes.FirstOrDefault() != null) { if (attributes.First().Description == description) { return (T)field.GetValue(null); } } } throw new ArgumentException(string.Format("{0} 未能找到對應的枚舉.", description), "Description"); } /// <summary> /// 獲取string獲取枚舉值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns></returns> public static T GetValue<T>(this string value) where T : struct { T result; if (Enum.TryParse(value, true, out result)) { return result; } throw new ArgumentException(string.Format("{0} 未能找到對應的枚舉.", value), "Value"); } }
再給個EnumHelper類吧:.net
using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.ComponentModel; using System.Web; namespace Vector.Framework.Tool { /// <summary> /// 程序說明:枚舉類型操做類 /// 創建者 :spw /// 創建日期:2018-04-25 /// </summary> public class EnumHelper { /// <summary> /// 經過枚舉類型獲得集合 /// </summary> /// <param name="type">集合類型</param> /// <param name="hasAll">是否包含請選擇</param> /// <returns></returns> public static List<ListItem> GetListItemByEnum(Type type, bool hasAll=true) { List<ListItem> list = new List<ListItem>(); FieldInfo[] fields = type.GetFields(); if (hasAll) { list.Add(new ListItem() { Value = "-1", Text = "請選擇" }); } for (int i = 1, count = fields.Length; i < count; i++) { list.Add(new ListItem() { Value = ((int)Enum.Parse(type, fields[i].Name)).ToString(), Text = fields[i].Name }); } return list; } #region 枚舉,值,串的相互轉化 /// <summary> /// 枚舉轉字符串 /// </summary> /// <typeparam name="T">枚舉類型</typeparam> /// <param name="t">枚舉對象</param> /// <returns></returns> private static string Enum2Text<T>(T t) { //string enumStringOne = color.ToString(); //效率低,不推薦 //string enumStringTwo = Enum.GetName(typeof(Color), color);//推薦 return Enum.GetName(typeof(T), t); } /// <summary> /// 枚舉轉值 /// </summary> /// <typeparam name="T">枚舉類型</typeparam> /// <param name="t">枚舉對象</param> /// <returns></returns> private static int Enum2Value<T>(T t) { //int enumValueOne = t.GetHashCode(); //int enumValueTwo = (int)color; //int enumValueThree = Convert.ToInt32(color); return t.GetHashCode(); } /// <summary> /// 字符串轉枚舉 /// </summary> /// <typeparam name="T">枚舉類型</typeparam> /// <param name="text">字符串</param> /// <returns></returns> private static T String2Enum<T>(string text) { //Color enumOne = (Color)Enum.Parse(typeof(Color), colorString); return (T)Enum.Parse(typeof(T), text); } /// <summary> /// 字符串轉值 /// </summary> /// <typeparam name="T">枚舉類型</typeparam> /// <param name="text">字符串</param> /// <returns></returns> public static int String2Value<T>(string text) { //int enumValueFour = (int)Enum.Parse(typeof(Color), colorString); return (int)Enum.Parse(typeof(T), text); } /// <summary> /// 值轉枚舉 /// </summary> /// <typeparam name="T">枚舉類型</typeparam> /// <param name="value">值</param> /// <returns></returns> private static T Value2Enum<T>(int value) { //Color enumTwo = (Color)colorValue; //Color enumThree = (Color)Enum.ToObject(typeof(Color), colorValue); return (T)Enum.ToObject(typeof(T), value); } /// <summary> /// 值轉字符串 /// </summary> /// <typeparam name="T">枚舉類型</typeparam> /// <param name="value">值</param> /// <returns></returns> public static string Value2Text<T>(int value) { //string enumStringThree = Enum.GetName(typeof(Color), colorValue); return Enum.GetName(typeof(T), value); } #endregion } public class ListItem { /// <summary> /// 顯示值 /// </summary> public string Text { get; set; } /// <summary> /// 實際值 /// </summary> public string Value { get; set; } /// <summary> /// 是否選中 /// </summary> public bool Selected { get; set; } } }
出處:https://blog.csdn.net/u011400752/article/details/83818832code
https://blog.csdn.net/spw55381155/article/details/80074326orm