EnumHelper.cs 666 B

1234567891011121314151617181920212223242526
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. namespace Aitex.Sorter.UI.ViewModel
  5. {
  6. public static class EnumHelper
  7. {
  8. public static Dictionary<T, string> ToDictionary<T>()
  9. {
  10. var result = new Dictionary<T, string>();
  11. var values = Enum.GetValues(typeof(T));
  12. foreach (T value in values)
  13. {
  14. var fi = value.GetType().GetField(value.ToString());
  15. var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
  16. if (attributes.Length > 0)
  17. result.Add(value, attributes[0].Description);
  18. else
  19. result.Add(value, value.ToString());
  20. }
  21. return result;
  22. }
  23. }
  24. }