Guest User

Untitled

a guest
Jul 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. public static class TypeInspectionExtensions
  2. {
  3. public static Type FindMostSpecificChildInterfaceOf<T>( this Type type )
  4. {
  5. var interfaceType = typeof( T );
  6. return type.FindMostSpecificChildInterfaceOf( interfaceType );
  7. }
  8.  
  9. public static Type FindMostSpecificChildInterfaceOf( this Type type, Type interfaceType )
  10. {
  11. if (!interfaceType.IsInterface)
  12. {
  13. throw new ArgumentException(
  14. String.Format( "{0} is not an interface type", interfaceType.FullName ), "interfaceType" );
  15. }
  16.  
  17. // NEED TO ADD MORE TESTS FOR GENERIC INTERFACE CHAINS
  18. if( type.IsInterface && type.Equals( interfaceType ) )
  19. {
  20. return type;
  21. }
  22. var interfaces = new List<Type>();
  23. interfaces.AddRange(
  24. type.GetInterfaces().Where(
  25. i => i.Equals( interfaceType )
  26. || i.Implements( interfaceType )
  27. || i.IsGenericType && interfaceType.IsGenericType && i.GetGenericTypeDefinition() == interfaceType.GetGenericTypeDefinition())
  28. );
  29.  
  30. if( interfaces.Count == 0)
  31. {
  32. return null;
  33. }
  34.  
  35. // The more specific interface will implement more interfaces that derive from the type being searched for
  36. var implementedInterfaces = interfaces.ToDictionary(
  37. t => t,
  38. t => t.GetInterfaces().Where( i => i.Implements( interfaceType ) ).ToList() );
  39. var mostImplementations = implementedInterfaces.Aggregate(
  40. implementedInterfaces.First(),
  41. ( currentMax, nextPair ) =>
  42. currentMax.Value.Count() > nextPair.Value.Count() ? currentMax : nextPair );
  43.  
  44. return mostImplementations.Key;
  45. }
  46.  
  47. public static bool Implements( this object implementingInstance, Type implementedType )
  48. {
  49. var implementingType = implementingInstance.GetType();
  50. return implementingType.Implements( implementedType );
  51. }
  52.  
  53. public static bool Implements<T>( this object implementingInstance )
  54. {
  55. var implementingType = implementingInstance.GetType();
  56. var implementedType = typeof( T );
  57. return implementingType.Implements( implementedType );
  58. }
  59.  
  60. public static bool Implements<T>( this Type implementingType )
  61. {
  62. Contract.Requires( () => implementingType );
  63. var implementedType = typeof( T );
  64. return implementingType.Implements( implementedType );
  65. }
  66.  
  67. public static bool Implements( this Type implementingType, Type implementedType )
  68. {
  69. if (implementedType.IsAssignableFrom(implementingType))
  70. {
  71. return true;
  72. }
  73.  
  74. if( implementedType.IsGenericTypeDefinition )
  75. {
  76. if (implementingType.IsGenericType && !implementingType.IsGenericTypeDefinition
  77. && implementingType.GetGenericTypeDefinition().Implements(implementedType))
  78. {
  79. return true;
  80. }
  81.  
  82. if (implementedType.IsInterface)
  83. {
  84. return implementingType.ImplementsGenericInterface( implementedType );
  85. }
  86.  
  87. return implementingType.ImplementsGenericClass( implementedType );
  88. }
  89.  
  90. return false;
  91. }
  92.  
  93. public static bool ImplementsGenericInterface( this Type implementingType, Type implementedType )
  94. {
  95. foreach (var @interface in implementingType.GetInterfaces())
  96. {
  97. if( @interface.IsGenericType && @interface.GetGenericTypeDefinition() == implementedType )
  98. {
  99. return true;
  100. }
  101. }
  102.  
  103. return false;
  104. }
  105.  
  106. public static bool ImplementsGenericClass( this Type implementingType, Type implementedType )
  107. {
  108. var type = implementingType;
  109. while (type != null)
  110. {
  111. if ( type.IsGenericType && type.GetGenericTypeDefinition() == implementedType )
  112. {
  113. return true;
  114. }
  115.  
  116. type = type.BaseType;
  117. }
  118.  
  119. return false;
  120. }
  121.  
  122. public static bool IsClosedGenericType( this Type type )
  123. {
  124. Contract.Requires( () => type );
  125. return type.IsGenericType && !type.IsGenericTypeDefinition;
  126. }
  127. }
Add Comment
Please, Sign In to add comment