EnumHelper.cs 383 B

1234567891011121314151617181920212223242526
  1. using System;
  2. namespace Common.Helper
  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. return (T) Enum.Parse(typeof (T), str);
  22. }
  23. }
  24. }