ReflectionHelpers.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #if UNITY_WSA && !UNITY_EDITOR && !ENABLE_IL2CPP
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Reflection;
  9. namespace System.TypeFix
  10. {
  11. public static class ReflectionHelpers
  12. {
  13. /// <summary>
  14. /// Determines whether the specified object is an instance of the current Type.
  15. /// </summary>
  16. /// <param name="type">The type.</param>
  17. /// <param name="o">The object to compare with the current type.</param>
  18. /// <returns>true if the current Type is in the inheritance hierarchy of the
  19. /// object represented by o, or if the current Type is an interface that o
  20. /// supports. false if neither of these conditions is the case, or if o is
  21. /// null, or if the current Type is an open generic type (that is,
  22. /// ContainsGenericParameters returns true).</returns>
  23. public static bool IsInstanceOfType(this Type type, object o)
  24. {
  25. return o != null && type.IsAssignableFrom(o.GetType());
  26. }
  27. internal static bool ImplementInterface(this Type type, Type ifaceType)
  28. {
  29. while (type != null)
  30. {
  31. Type[] interfaces = type.GetTypeInfo().ImplementedInterfaces.ToArray(); // .GetInterfaces();
  32. if (interfaces != null)
  33. {
  34. for (int i = 0; i < interfaces.Length; i++)
  35. {
  36. if (interfaces[i] == ifaceType || (interfaces[i] != null && interfaces[i].ImplementInterface(ifaceType)))
  37. {
  38. return true;
  39. }
  40. }
  41. }
  42. type = type.GetTypeInfo().BaseType;
  43. // type = type.BaseType;
  44. }
  45. return false;
  46. }
  47. public static bool IsAssignableFrom(this Type type, Type c)
  48. {
  49. if (c == null)
  50. {
  51. return false;
  52. }
  53. if (type == c)
  54. {
  55. return true;
  56. }
  57. //RuntimeType runtimeType = type.UnderlyingSystemType as RuntimeType;
  58. //if (runtimeType != null)
  59. //{
  60. // return runtimeType.IsAssignableFrom(c);
  61. //}
  62. //if (c.IsSubclassOf(type))
  63. if (c.GetTypeInfo().IsSubclassOf(c))
  64. {
  65. return true;
  66. }
  67. //if (type.IsInterface)
  68. if (type.GetTypeInfo().IsInterface)
  69. {
  70. return c.ImplementInterface(type);
  71. }
  72. if (type.IsGenericParameter)
  73. {
  74. Type[] genericParameterConstraints = type.GetTypeInfo().GetGenericParameterConstraints();
  75. for (int i = 0; i < genericParameterConstraints.Length; i++)
  76. {
  77. if (!genericParameterConstraints[i].IsAssignableFrom(c))
  78. {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. return false;
  85. }
  86. public static bool IsEnum(this Type type)
  87. {
  88. return type.GetTypeInfo().IsEnum;
  89. }
  90. }
  91. }
  92. #endif
  93. #endif