Enums.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Text;
  4. //#if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT || PORTABLE
  5. using System.Collections;
  6. using System.Reflection;
  7. //#endif
  8. using Org.BouncyCastle.Utilities.Date;
  9. #if UNITY_WSA && !UNITY_EDITOR && !ENABLE_IL2CPP
  10. using System.TypeFix;
  11. #endif
  12. namespace Org.BouncyCastle.Utilities
  13. {
  14. internal abstract class Enums
  15. {
  16. internal static Enum GetEnumValue(System.Type enumType, string s)
  17. {
  18. /*if (!enumType.IsEnum)
  19. throw new ArgumentException("Not an enumeration type", "enumType");*/
  20. // We only want to parse single named constants
  21. if (s.Length > 0 && char.IsLetter(s[0]) && s.IndexOf(',') < 0)
  22. {
  23. s = s.Replace('-', '_');
  24. s = s.Replace('/', '_');
  25. #if NETCF_1_0
  26. FieldInfo field = enumType.GetField(s, BindingFlags.Static | BindingFlags.Public);
  27. if (field != null)
  28. {
  29. return (Enum)field.GetValue(null);
  30. }
  31. #else
  32. return (Enum)Enum.Parse(enumType, s, false);
  33. #endif
  34. }
  35. throw new ArgumentException();
  36. }
  37. internal static Array GetEnumValues(System.Type enumType)
  38. {
  39. /*if (!enumType.IsEnum)
  40. throw new ArgumentException("Not an enumeration type", "enumType");*/
  41. #if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT
  42. IList result = Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  43. FieldInfo[] fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
  44. foreach (FieldInfo field in fields)
  45. {
  46. // Note: Argument to GetValue() ignored since the fields are static,
  47. // but Silverlight for Windows Phone throws exception if we pass null
  48. result.Add(field.GetValue(enumType));
  49. }
  50. object[] arr = new object[result.Count];
  51. result.CopyTo(arr, 0);
  52. return arr;
  53. #else
  54. return Enum.GetValues(enumType);
  55. #endif
  56. }
  57. internal static Enum GetArbitraryValue(System.Type enumType)
  58. {
  59. Array values = GetEnumValues(enumType);
  60. int pos = (int)(DateTimeUtilities.CurrentUnixMs() & int.MaxValue) % values.Length;
  61. return (Enum)values.GetValue(pos);
  62. }
  63. internal static bool IsEnumType(System.Type t)
  64. {
  65. #if NEW_REFLECTION || NETFX_CORE
  66. return t.GetTypeInfo().IsEnum;
  67. #else
  68. return t.IsEnum;
  69. #endif
  70. }
  71. }
  72. }
  73. #endif