Reflection.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if CRIPPLED_REFLECTION
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace System.Reflection
  8. {
  9. [Flags]
  10. internal enum BindingFlags
  11. {
  12. Instance = 4,
  13. Static = 8,
  14. Public = 16,
  15. NonPublic = 32,
  16. }
  17. }
  18. namespace System
  19. {
  20. internal static class TypeExtensions
  21. {
  22. public static bool IsNestedPrivate(this Type t)
  23. {
  24. return t.GetTypeInfo().IsNestedPrivate;
  25. }
  26. public static bool IsInterface(this Type t)
  27. {
  28. return t.GetTypeInfo().IsInterface;
  29. }
  30. public static bool IsGenericType(this Type t)
  31. {
  32. return t.GetTypeInfo().IsGenericType;
  33. }
  34. public static Type GetBaseType(this Type t)
  35. {
  36. return t.GetTypeInfo().BaseType;
  37. }
  38. public static Type[] GetGenericArguments(this Type t)
  39. {
  40. // TODO: check what's the right way to support this
  41. return t.GetTypeInfo().GenericTypeParameters.ToArray();
  42. }
  43. public static Type[] GetInterfaces(this Type t)
  44. {
  45. return t.GetTypeInfo().ImplementedInterfaces.ToArray();
  46. }
  47. public static bool IsAssignableFrom(this Type t1, Type t2)
  48. {
  49. return t1.GetTypeInfo().IsAssignableFrom(t2.GetTypeInfo());
  50. }
  51. public static MethodInfo[] GetMethods(this Type t, BindingFlags flags)
  52. {
  53. return t.GetTypeInfo().DeclaredMethods.Where(m => IsVisible(m, flags)).ToArray();
  54. }
  55. private static bool IsVisible(MethodInfo method, BindingFlags flags)
  56. {
  57. if ((flags & BindingFlags.Public) != 0 != method.IsPublic)
  58. {
  59. return false;
  60. }
  61. if ((flags & BindingFlags.NonPublic) == 0 && !method.IsPublic)
  62. {
  63. return false;
  64. }
  65. if ((flags & BindingFlags.Static) != 0 != method.IsStatic)
  66. {
  67. return false;
  68. }
  69. return true;
  70. }
  71. }
  72. }
  73. #else
  74. namespace System
  75. {
  76. internal static class TypeExtensions
  77. {
  78. public static bool IsNestedPrivate(this Type t)
  79. {
  80. return t.IsNestedPrivate;
  81. }
  82. public static bool IsInterface(this Type t)
  83. {
  84. return t.IsInterface;
  85. }
  86. public static bool IsGenericType(this Type t)
  87. {
  88. return t.IsGenericType;
  89. }
  90. public static Type GetBaseType(this Type t)
  91. {
  92. return t.BaseType;
  93. }
  94. }
  95. }
  96. #endif