1+ namespace TestStack . ConventionTests . ConventionData
2+ {
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
6+ using System . Reflection ;
7+
8+ public static class TypeExtensions
9+ {
10+ public static bool IsConcreteClass ( this Type t )
11+ {
12+ return t . IsClass && ! t . IsAbstract ;
13+ }
14+
15+ public static bool IsEnum ( this Type type )
16+ {
17+ return typeof ( Enum ) . IsAssignableFrom ( type ) ;
18+ }
19+
20+ public static bool IsStatic ( this Type type )
21+ {
22+ return type . IsClass && ! ( type . IsSealed && type . IsAbstract ) ;
23+ }
24+
25+ public static bool HasDefaultConstructor ( this Type type )
26+ {
27+ return type . GetConstructors ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . NonPublic |
28+ BindingFlags . DeclaredOnly )
29+ . Any ( constructorInfo => constructorInfo . GetParameters ( ) . Length == 0 && ! constructorInfo . IsPrivate ) ;
30+ }
31+
32+ public static bool HasPublicDefaultConstructor ( this Type type )
33+ {
34+ return type . GetConstructors ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . DeclaredOnly )
35+ . Any ( constructorInfo => constructorInfo . GetParameters ( ) . Length == 0 ) ;
36+ }
37+
38+ public static bool AssignableTo < TAssignableTo > ( this Type type )
39+ {
40+ return typeof ( TAssignableTo ) . IsAssignableFrom ( type ) ;
41+ }
42+
43+ public static IEnumerable < MethodInfo > NonVirtualMethods ( this Type type )
44+ {
45+ var methodInfos =
46+ type . GetMethods ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . NonPublic |
47+ BindingFlags . DeclaredOnly ) ;
48+ return methodInfos . Where ( methodInfo =>
49+ ! methodInfo . IsPrivate &&
50+ methodInfo . DeclaringType == type &&
51+ ! methodInfo . Name . StartsWith ( "<" ) )
52+ . Where ( methodInfo => methodInfo . Name != "Equals" )
53+ . Where ( methodInfo => ! methodInfo . IsVirtual || methodInfo . IsFinal ) ;
54+ }
55+
56+ public static IEnumerable < Type > GetClosedInterfacesOf ( this Type type , Type openGeneric )
57+ {
58+ return from i in type . GetInterfaces ( )
59+ where i . IsGenericType
60+ let defn = i . GetGenericTypeDefinition ( )
61+ where defn == openGeneric
62+ select i ;
63+ }
64+
65+ public static bool ClosesInterface ( this Type t , Type openGeneric )
66+ {
67+ return t . GetClosedInterfacesOf ( openGeneric ) . Any ( ) ;
68+ }
69+ }
70+ }
0 commit comments