@@ -16,10 +16,10 @@ internal static class TypeExtensions
1616 /// ((IList)myList).CopyToList(targetType).
1717 /// </code>
1818 /// </summary>
19- public static IList CopyToList ( this IEnumerable source , Type type )
19+ public static IList CopyToList ( this IEnumerable copyFrom , Type elementType )
2020 {
21- Type collectionType = typeof ( List < > ) . MakeGenericType ( type ) ;
22- return ( IList ) CopyToTypedCollection ( source , collectionType ) ;
21+ Type collectionType = typeof ( List < > ) . MakeGenericType ( elementType ) ;
22+ return ( IList ) CopyToTypedCollection ( copyFrom , collectionType ) ;
2323 }
2424
2525 /// <summary>
@@ -34,7 +34,7 @@ public static IEnumerable CopyToTypedCollection(this IEnumerable source, Type co
3434 if ( collectionType == null ) throw new ArgumentNullException ( nameof ( collectionType ) ) ;
3535
3636 var concreteCollectionType = collectionType . ToConcreteCollectionType ( ) ;
37- dynamic concreteCollectionInstance = concreteCollectionType . New < dynamic > ( ) ;
37+ dynamic concreteCollectionInstance = TypeHelper . CreateInstance ( concreteCollectionType ) ;
3838
3939 foreach ( var item in source )
4040 {
@@ -44,80 +44,29 @@ public static IEnumerable CopyToTypedCollection(this IEnumerable source, Type co
4444 return concreteCollectionInstance ;
4545 }
4646
47- /// <summary>
48- /// Creates a List{TInterface} where TInterface is the generic for type specified by t
49- /// </summary>
50- public static IEnumerable GetEmptyCollection ( this Type t )
51- {
52- if ( t == null ) throw new ArgumentNullException ( nameof ( t ) ) ;
53-
54- var listType = typeof ( List < > ) . MakeGenericType ( t ) ;
55- var list = ( IEnumerable ) CreateNewInstance ( listType ) ;
56- return list ;
57- }
58-
5947 public static string GetResourceStringId < TResource , TId > ( TId id ) where TResource : class , IIdentifiable < TId >
6048 {
61- var tempResource = typeof ( TResource ) . New < TResource > ( ) ;
49+ var tempResource = TypeHelper . CreateInstance < TResource > ( ) ;
6250 tempResource . Id = id ;
6351 return tempResource . StringId ;
6452 }
6553
66- public static object New ( this Type t )
67- {
68- return New < object > ( t ) ;
69- }
70-
7154 /// <summary>
72- /// Creates a new instance of type t, casting it to the specified type .
55+ /// Whether the specified source type implements or equals the specified interface .
7356 /// </summary>
74- public static T New < T > ( this Type t )
57+ public static bool IsOrImplementsInterface ( this Type source , Type interfaceType )
7558 {
76- if ( t == null ) throw new ArgumentNullException ( nameof ( t ) ) ;
77-
78- var instance = ( T ) CreateNewInstance ( t ) ;
79- return instance ;
80- }
81-
82- private static object CreateNewInstance ( Type type )
83- {
84- try
59+ if ( interfaceType == null )
8560 {
86- return Activator . CreateInstance ( type ) ;
61+ throw new ArgumentNullException ( nameof ( interfaceType ) ) ;
8762 }
88- catch ( Exception exception )
63+
64+ if ( source == null )
8965 {
90- throw new InvalidOperationException ( $ "Failed to create an instance of ' { type . FullName } ' using its default constructor." , exception ) ;
66+ return false ;
9167 }
92- }
93-
94- /// <summary>
95- /// Whether or not a type implements an interface.
96- /// </summary>
97- public static bool Implements < T > ( this Type concreteType )
98- => Implements ( concreteType , typeof ( T ) ) ;
99-
100- /// <summary>
101- /// Whether or not a type implements an interface.
102- /// </summary>
103- public static bool Implements ( this Type concreteType , Type interfaceType )
104- => interfaceType ? . IsAssignableFrom ( concreteType ) == true ;
10568
106- /// <summary>
107- /// Whether or not a type inherits a base type.
108- /// </summary>
109- public static bool Inherits < T > ( this Type concreteType )
110- => Inherits ( concreteType , typeof ( T ) ) ;
111-
112- /// <summary>
113- /// Whether or not a type inherits a base type.
114- /// </summary>
115- public static bool Inherits ( this Type concreteType , Type interfaceType )
116- => interfaceType ? . IsAssignableFrom ( concreteType ) == true ;
117-
118- public static bool ImplementsInterface ( this Type source , Type interfaceType )
119- {
120- return source . GetInterfaces ( ) . Any ( type => type == interfaceType ) ;
69+ return source == interfaceType || source . GetInterfaces ( ) . Any ( type => type == interfaceType ) ;
12170 }
12271 }
12372}
0 commit comments