PortableReflectionExtensions.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace Caliburn.Micro.Core
  6. {
  7. /// <summary>
  8. /// A collection of extension methods to help with differing reflection between the portable library and SL5
  9. /// </summary>
  10. internal static class PortableReflectionExtensions {
  11. public static bool IsAssignableFrom(this Type t, Type c) {
  12. return t.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo());
  13. }
  14. public static Type[] GetGenericArguments(this Type t) {
  15. return t.GetTypeInfo().GenericTypeArguments;
  16. }
  17. public static IEnumerable<PropertyInfo> GetProperties(this Type t) {
  18. return t.GetRuntimeProperties();
  19. }
  20. public static IEnumerable<ConstructorInfo> GetConstructors(this Type t) {
  21. return t.GetTypeInfo().DeclaredConstructors;
  22. }
  23. public static IEnumerable<Type> GetInterfaces(this Type t) {
  24. return t.GetTypeInfo().ImplementedInterfaces;
  25. }
  26. public static IEnumerable<Type> GetTypes(this Assembly a) {
  27. return a.DefinedTypes.Select(t => t.AsType());
  28. }
  29. public static bool IsAbstract(this Type t) {
  30. return t.GetTypeInfo().IsAbstract;
  31. }
  32. public static bool IsInterface(this Type t) {
  33. return t.GetTypeInfo().IsInterface;
  34. }
  35. public static bool IsGenericType(this Type t) {
  36. return t.GetTypeInfo().IsGenericType;
  37. }
  38. public static MethodInfo GetMethod(this Type t, string name, Type[] parameters) {
  39. return t.GetRuntimeMethod(name, parameters);
  40. }
  41. }
  42. }