123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace WMS.BZModels
- {
- public static class Extensions
- {
- #region 获取枚举的描述
- /// <summary>
- /// 获取枚举值对应的描述
- /// </summary>
- /// <param name="enumType"></param>
- /// <returns></returns>
- public static string GetDescription(this System.Enum enumType)
- {
- var value = enumType.ToString();
- if (value.IndexOf(',') > 0)
- {
- var lists = value.Split(',');
- var strings = new List<string>();
- foreach (var item in lists)
- {
- //var itemenum = (WCS.Entity.Protocol.Station.StationStatus)Enum.Parse(typeof(StationStatus), item);
- FieldInfo itemEnumInfo = enumType.GetType().GetField(item.Trim());
- if (itemEnumInfo != null)
- {
- DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])itemEnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
- if (EnumAttributes.Length > 0)
- {
- strings.Add(EnumAttributes[0].Description);
- }
- }
- else
- {
- strings.Add(item);
- }
- }
- return string.Join(",", strings.ToArray());
- }
- else
- {
- FieldInfo EnumInfo = enumType.GetType().GetField(enumType.ToString());
- if (EnumInfo != null)
- {
- DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
- if (EnumAttributes.Length > 0)
- {
- return EnumAttributes[0].Description;
- }
- }
- return enumType.ToString();
- }
- }
- /// <summary>
- /// 将枚举转为集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static List<EnumEntity> EnumToList<T>()
- {
- List<EnumEntity> list = new List<EnumEntity>();
- foreach (var e in Enum.GetValues(typeof(T)))
- {
- EnumEntity m = new EnumEntity();
- object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
- if (objArr != null && objArr.Length > 0)
- {
- DescriptionAttribute da = objArr[0] as DescriptionAttribute;
- m.text = da.Description;
- }
- else
- {
- m.text = e.ToString();
- }
- m.id = Convert.ToInt32(e);
- list.Add(m);
- }
- return list;
- }
- public static List<EnumEntity2> EnumToListDesc<T>()
- {
- List<EnumEntity2> list = new List<EnumEntity2>();
- foreach (var e in Enum.GetValues(typeof(T)))
- {
- EnumEntity2 m = new EnumEntity2();
- object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
-
- if (objArr != null && objArr.Length > 0)
- {
- DescriptionAttribute da = objArr[0] as DescriptionAttribute;
- m.text = da.Description;
- m.id = da.Description;
- }
- //else
- //{
- // m.text = e.ToString();
- //}
- //m.id = da.Description;
- list.Add(m);
- }
- return list;
- }
- public static List<EnumEntity2> EnumToListIdDesc<T>()
- {
- List<EnumEntity2> list = new List<EnumEntity2>();
- foreach (var e in Enum.GetValues(typeof(T)))
- {
- EnumEntity2 m = new EnumEntity2();
- object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
- if (objArr != null && objArr.Length > 0)
- {
- DescriptionAttribute da = objArr[0] as DescriptionAttribute;
- m.text = da.Description;
-
- }
- //else
- //{
- // m.text = e.ToString();
- //}
- m.id = e.ToString();
- list.Add(m);
- }
- return list;
- }
- #endregion
- }
- /// <summary>
- /// 枚举实体
- /// </summary>
- public class EnumEntity
- {
- /// <summary>
- /// 枚举对象的值
- /// </summary>
- public int id { set; get; }
- /// <summary>
- /// 枚举的描述
- /// </summary>
- public string text { set; get; }
-
-
- }
- public class EnumEntity2
- {
- /// <summary>
- /// 枚举对象的值
- /// </summary>
- public string id { set; get; }
- /// <summary>
- /// 枚举的描述
- /// </summary>
- public string text { set; get; }
-
- }
- }
|