人民医院API

EnumHelper.cs 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace RMYY_CallCenter_Api.Utility
  9. {
  10. public static class EnumHelper
  11. {
  12. /// <summary>
  13. /// 返回枚举项的描述信息。
  14. /// </summary>
  15. /// <param name="value">要获取描述信息的枚举项。</param>
  16. /// <returns>枚举想的描述信息。</returns>
  17. public static string GetDescription(Enum value)
  18. {
  19. Type enumType = value.GetType();
  20. // 获取枚举常数名称。
  21. string name = Enum.GetName(enumType, value);
  22. if (name != null)
  23. {
  24. // 获取枚举字段。
  25. FieldInfo fieldInfo = enumType.GetField(name);
  26. if (fieldInfo != null)
  27. {
  28. // 获取描述的属性。
  29. DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
  30. typeof(DescriptionAttribute), false) as DescriptionAttribute;
  31. if (attr != null)
  32. {
  33. return attr.Description;
  34. }
  35. }
  36. }
  37. return null;
  38. }
  39. /// <summary>
  40. /// 枚举参数
  41. /// </summary>
  42. /// <typeparam name="T"></typeparam>
  43. /// <returns></returns>
  44. public static List<T> ToList<T>()
  45. {
  46. return Enum.GetValues(typeof(T)).Cast<T>().ToList();
  47. }
  48. }
  49. }