人民医院API

EnumHelper.cs 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. }
  40. }