Enums.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Date;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities
  6. {
  7. internal static class Enums
  8. {
  9. internal static TEnum GetEnumValue<TEnum>(string s)
  10. where TEnum : struct, Enum
  11. {
  12. // We only want to parse single named constants
  13. if (s.Length > 0 && char.IsLetter(s[0]) && s.IndexOf(',') < 0)
  14. {
  15. s = s.Replace('-', '_');
  16. s = s.Replace('/', '_');
  17. #if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  18. return Enum.Parse<TEnum>(s, false);
  19. #else
  20. return (TEnum)Enum.Parse(typeof(TEnum), s, false);
  21. #endif
  22. }
  23. throw new ArgumentException();
  24. }
  25. internal static TEnum[] GetEnumValues<TEnum>()
  26. where TEnum : struct, Enum
  27. {
  28. #if NET5_0_OR_GREATER
  29. return Enum.GetValues<TEnum>();
  30. #else
  31. return (TEnum[])Enum.GetValues(typeof(TEnum));
  32. #endif
  33. }
  34. internal static TEnum GetArbitraryValue<TEnum>()
  35. where TEnum : struct, Enum
  36. {
  37. TEnum[] values = GetEnumValues<TEnum>();
  38. int pos = (int)(DateTimeUtilities.CurrentUnixMs() & int.MaxValue) % values.Length;
  39. return values[pos];
  40. }
  41. }
  42. }
  43. #pragma warning restore
  44. #endif