EnumHelper.cs 500 B

123456789101112131415161718192021222324252627282930
  1. using System;
  2. namespace ET
  3. {
  4. public static class EnumHelper
  5. {
  6. public static int EnumIndex<T>(int value)
  7. {
  8. int i = 0;
  9. foreach (object v in Enum.GetValues(typeof (T)))
  10. {
  11. if ((int) v == value)
  12. {
  13. return i;
  14. }
  15. ++i;
  16. }
  17. return -1;
  18. }
  19. public static T FromString<T>(string str)
  20. {
  21. if (!Enum.IsDefined(typeof(T), str))
  22. {
  23. return default(T);
  24. }
  25. return (T)Enum.Parse(typeof(T), str);
  26. }
  27. }
  28. }