@@ -151,9 +151,13 @@ public static bool Enumerable(Type type)
151
151
{
152
152
return type . IsArray || ( type != typeof ( string ) && AssignableTo ( type , typeof ( System . Collections . IEnumerable ) ) ) ;
153
153
}
154
- public static bool AssignableTo ( Type assignableType , Type type )
154
+ public static bool AssignableTo ( Type fromType , Type toType )
155
155
{
156
- return type ? . IsAssignableFrom ( assignableType ) ?? false ;
156
+ if ( fromType == null || toType == null ) return false ;
157
+
158
+ return toType . IsGenericTypeDefinition
159
+ ? toType . IsAssignableTo ( fromType )
160
+ : fromType . IsAssignableFrom ( toType ) ;
157
161
}
158
162
public static bool CanBeNull ( Type type )
159
163
{
@@ -285,5 +289,45 @@ public static bool ReadOnlyMemory(Type type, [NotNullWhen(true)] out Type? eleme
285
289
elementType = null ;
286
290
return false ;
287
291
}
292
+ public static bool AssignableToOpenGeneric ( Type type , Type elementType )
293
+ {
294
+ if ( elementType . IsInterface )
295
+ {
296
+ return ImplementationOfOpenGeneric ( type , elementType ) ;
297
+ }
288
298
299
+ return type == elementType || DerivedFromOpenGeneric ( type , elementType ) ;
300
+ }
301
+ public static bool ImplementationOfOpenGeneric ( Type type , Type elementType )
302
+ {
303
+ if ( type . IsInterface && type . IsGenericType &&
304
+ type . GetGenericTypeDefinition ( ) == elementType )
305
+ {
306
+ return true ;
307
+ }
308
+ return type . GetInterfaces ( )
309
+ . Any ( i => i . IsGenericType && i . GetGenericTypeDefinition ( ) == elementType ) ;
310
+ }
311
+ public static bool DerivedFromOpenGeneric ( Type ? type , Type ? elementType )
312
+ {
313
+ if ( type == null || elementType == null )
314
+ {
315
+ return false ;
316
+ }
317
+
318
+ if ( type == elementType )
319
+ {
320
+ return false ;
321
+ }
322
+
323
+ for ( Type ? baseType = type ; baseType is not null ; baseType = baseType . BaseType )
324
+ {
325
+ if ( baseType . IsGenericType && baseType . GetGenericTypeDefinition ( ) == elementType )
326
+ {
327
+ return true ;
328
+ }
329
+ }
330
+
331
+ return false ;
332
+ }
289
333
}
0 commit comments