EnumUtil.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using DocumentFormat.OpenXml.Vml;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Aitex.Core.Utilities
  8. {
  9. public class EnumLoop<Key> where Key : struct, IConvertible
  10. {
  11. static readonly Key[] arr = (Key[])Enum.GetValues(typeof(Key));
  12. public static void ForEach(Action<Key> act)
  13. {
  14. for (int i = 0; i < arr.Length; i++)
  15. {
  16. act(arr[i]);
  17. }
  18. }
  19. }
  20. public class EnumUtil
  21. {
  22. /// <summary>
  23. /// 获取枚举的描述
  24. /// </summary>
  25. /// <param name="val"></param>
  26. /// <returns></returns>
  27. public static string GetEnumDescription(Enum val)
  28. {
  29. var field = val.GetType().GetField(val.ToString());
  30. var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
  31. return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
  32. }
  33. }
  34. }