Reflection.cs 2.6 KB

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