Guest User

Untitled

a guest
Jun 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public enum MyEnum
  2. {
  3. One=1,
  4. Two=2,
  5. Three=3
  6. }
  7.  
  8. Enum.GetNames(typeof(MyEnum))
  9. .Select(exEnum =>
  10. (MyEnum)Enum.Parse(typeof(MyEnum), exEnum))
  11. .ToList();
  12.  
  13. Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>();
  14.  
  15. public static class MyGlobals
  16. {
  17. public static readonly List<MyEnum> EnumList =
  18. Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().ToList();
  19. }
  20.  
  21. public static List<T> EnumToList<T>()
  22.  
  23. {
  24.  
  25. Type enumType = typeof(T);
  26.  
  27. // Can't use type constraints on value types, so have to do check like this
  28.  
  29. if (enumType.BaseType != typeof(Enum))
  30.  
  31. throw new ArgumentException("T must be of type System.Enum");
  32.  
  33. return new List<T>(Enum.GetValues(enumType) as IEnumerable<T>);
  34.  
  35. }
Add Comment
Please, Sign In to add comment