废话不多说,看代码
1、先定义枚举类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/// <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, } |
2、添加EnumHelper类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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"); } } |
3、另一个版本的EnumHelper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.ComponentModel; using System.Web; namespace Misc.Helpers { 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; } } } |