Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class TypeInspectionExtensions
- {
- public static Type FindMostSpecificChildInterfaceOf<T>( this Type type )
- {
- var interfaceType = typeof( T );
- return type.FindMostSpecificChildInterfaceOf( interfaceType );
- }
- public static Type FindMostSpecificChildInterfaceOf( this Type type, Type interfaceType )
- {
- if (!interfaceType.IsInterface)
- {
- throw new ArgumentException(
- String.Format( "{0} is not an interface type", interfaceType.FullName ), "interfaceType" );
- }
- // NEED TO ADD MORE TESTS FOR GENERIC INTERFACE CHAINS
- if( type.IsInterface && type.Equals( interfaceType ) )
- {
- return type;
- }
- var interfaces = new List<Type>();
- interfaces.AddRange(
- type.GetInterfaces().Where(
- i => i.Equals( interfaceType )
- || i.Implements( interfaceType )
- || i.IsGenericType && interfaceType.IsGenericType && i.GetGenericTypeDefinition() == interfaceType.GetGenericTypeDefinition())
- );
- if( interfaces.Count == 0)
- {
- return null;
- }
- // The more specific interface will implement more interfaces that derive from the type being searched for
- var implementedInterfaces = interfaces.ToDictionary(
- t => t,
- t => t.GetInterfaces().Where( i => i.Implements( interfaceType ) ).ToList() );
- var mostImplementations = implementedInterfaces.Aggregate(
- implementedInterfaces.First(),
- ( currentMax, nextPair ) =>
- currentMax.Value.Count() > nextPair.Value.Count() ? currentMax : nextPair );
- return mostImplementations.Key;
- }
- public static bool Implements( this object implementingInstance, Type implementedType )
- {
- var implementingType = implementingInstance.GetType();
- return implementingType.Implements( implementedType );
- }
- public static bool Implements<T>( this object implementingInstance )
- {
- var implementingType = implementingInstance.GetType();
- var implementedType = typeof( T );
- return implementingType.Implements( implementedType );
- }
- public static bool Implements<T>( this Type implementingType )
- {
- Contract.Requires( () => implementingType );
- var implementedType = typeof( T );
- return implementingType.Implements( implementedType );
- }
- public static bool Implements( this Type implementingType, Type implementedType )
- {
- if (implementedType.IsAssignableFrom(implementingType))
- {
- return true;
- }
- if( implementedType.IsGenericTypeDefinition )
- {
- if (implementingType.IsGenericType && !implementingType.IsGenericTypeDefinition
- && implementingType.GetGenericTypeDefinition().Implements(implementedType))
- {
- return true;
- }
- if (implementedType.IsInterface)
- {
- return implementingType.ImplementsGenericInterface( implementedType );
- }
- return implementingType.ImplementsGenericClass( implementedType );
- }
- return false;
- }
- public static bool ImplementsGenericInterface( this Type implementingType, Type implementedType )
- {
- foreach (var @interface in implementingType.GetInterfaces())
- {
- if( @interface.IsGenericType && @interface.GetGenericTypeDefinition() == implementedType )
- {
- return true;
- }
- }
- return false;
- }
- public static bool ImplementsGenericClass( this Type implementingType, Type implementedType )
- {
- var type = implementingType;
- while (type != null)
- {
- if ( type.IsGenericType && type.GetGenericTypeDefinition() == implementedType )
- {
- return true;
- }
- type = type.BaseType;
- }
- return false;
- }
- public static bool IsClosedGenericType( this Type type )
- {
- Contract.Requires( () => type );
- return type.IsGenericType && !type.IsGenericTypeDefinition;
- }
- }
Add Comment
Please, Sign In to add comment