¶Ô±ÈÐÂÎļþ |
| | |
| | | using System.ComponentModel; |
| | | using System.Reflection; |
| | | |
| | | namespace WIDESEA_Core.Enums |
| | | { |
| | | public static class EnumHelper |
| | | { |
| | | /// <summary> |
| | | /// æä¸¾è½¬åå
¸éå |
| | | /// </summary> |
| | | /// <typeparam name="T">æä¸¾ç±»åç§°</typeparam> |
| | | /// <param name="keyDefault">é»è®¤keyå¼</param> |
| | | /// <param name="valueDefault">é»è®¤valueå¼</param> |
| | | /// <returns>è¿åçæçåå
¸éå</returns> |
| | | public static Dictionary<string, object> EnumListDic<T>(string keyDefault, string valueDefault = "") |
| | | { |
| | | Dictionary<string, object> dicEnum = new Dictionary<string, object>(); |
| | | Type enumType = typeof(T); |
| | | if (!enumType.IsEnum) |
| | | { |
| | | return dicEnum; |
| | | } |
| | | if (!string.IsNullOrEmpty(keyDefault)) //夿æ¯å¦æ·»å é»è®¤é项 |
| | | { |
| | | dicEnum.Add(keyDefault, valueDefault); |
| | | } |
| | | string[] fieldstrs = Enum.GetNames(enumType); //è·åæä¸¾å段æ°ç» |
| | | foreach (var item in fieldstrs) |
| | | { |
| | | string description = string.Empty; |
| | | var field = enumType.GetField(item); |
| | | object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //è·å屿§å段æ°ç» |
| | | if (arr != null && arr.Length > 0) |
| | | { |
| | | description = ((DescriptionAttribute)arr[0]).Description; //屿§æè¿° |
| | | } |
| | | else |
| | | { |
| | | description = item; //æè¿°ä¸åå¨ååæ®µåç§° |
| | | } |
| | | dicEnum.Add(description, (int)Enum.Parse(enumType, item)); //ä¸ç¨æä¸¾çvalueå¼ä½ä¸ºåå
¸keyå¼çåå 仿䏾ä¾åè½çåºæ¥ï¼å
¶å®è¿è¾¹åºè¯¥å¤æä»çå¼ä¸åå¨ï¼é»è®¤ååæ®µåç§° |
| | | } |
| | | return dicEnum; |
| | | } |
| | | /// <summary> |
| | | /// è·åæä¸¾é¡¹æè¿°ä¿¡æ¯ ä¾å¦GetEnumDesc(Days.Sunday) |
| | | /// </summary> |
| | | /// <param name="en">æä¸¾é¡¹ å¦Days.Sunday</param> |
| | | /// <returns></returns> |
| | | public static string GetIntegralRuleTypeEnumDesc(this Enum en) |
| | | { |
| | | Type type = en.GetType(); |
| | | MemberInfo[] memInfo = type.GetMember(en.ToString()); |
| | | if (memInfo != null && memInfo.Length > 0) |
| | | { |
| | | object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); |
| | | if (attrs != null && attrs.Length > 0) |
| | | return ((DescriptionAttribute)attrs[0]).Description; |
| | | } |
| | | return en.ToString(); |
| | | } |
| | | /// <summary> |
| | | /// è·åæä¸¾éå |
| | | /// </summary> |
| | | /// <typeparam name="T">æä¸¾ç±»åç§°</typeparam> |
| | | /// <returns></returns> |
| | | public static IEnumerable<EnumModel> GetEnumList<T>() |
| | | { |
| | | var model = default(T); |
| | | FieldInfo[] fieldinfo = typeof(T).GetFields(); |
| | | List<EnumModel> result = new List<EnumModel>(); |
| | | foreach (FieldInfo field in fieldinfo) |
| | | { |
| | | EnumModel enumModel = new EnumModel(); |
| | | if (!(Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute)) |
| | | { |
| | | enumModel.Desc = field.GetValue(model).ToString(); |
| | | } |
| | | else |
| | | { |
| | | enumModel.Desc = attribute.Description; |
| | | } |
| | | enumModel.Value = field.GetValue(model).GetHashCode(); |
| | | enumModel.Key = field.GetValue(model) as ValueType; |
| | | if (field.GetValue(model).ToString() != "0") |
| | | { |
| | | result.Add(enumModel); |
| | | } |
| | | |
| | | } |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | public class EnumModel |
| | | { |
| | | /// <summary> |
| | | /// Enumçå¼ |
| | | /// </summary> |
| | | public int Value { get; set; } |
| | | /// <summary> |
| | | /// Enumçkey |
| | | /// </summary> |
| | | public ValueType Key { get; set; } |
| | | /// <summary> |
| | | /// Enumæè¿° |
| | | /// </summary> |
| | | public string Desc { get; set; } |
| | | } |
| | | |
| | | } |