@@ -220,8 +220,7 @@ public static void MinLength(
220
220
}
221
221
222
222
/// <summary>
223
- /// Ensures that the type argument is of the expected type. Supports testing for
224
- /// open generic types.
223
+ /// Ensures that the type argument is of the expected type.
225
224
/// </summary>
226
225
/// <param name="type">The <see cref="Type"/> argument.</param>
227
226
/// <param name="expectedType">The expected <see cref="Type"/>.</param>
@@ -230,12 +229,13 @@ public static void MinLength(
230
229
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
231
230
[ StackTraceHidden ]
232
231
public static void OfType (
233
- [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
234
232
Type ? type ,
235
- Type ? expectedType ,
233
+ Type expectedType ,
236
234
[ CallerArgumentExpression ( "type" ) ] string ? paramName = null )
237
235
{
238
- if ( ! IsAssignableTo ( type , expectedType ) )
236
+ Ensure . Arg . NotNull ( expectedType ) ;
237
+
238
+ if ( type != null && ! expectedType . IsAssignableFrom ( type ) )
239
239
{
240
240
throw new ArgumentException (
241
241
$ "Argument '{ paramName } ' is of type '{ type } ' but expected to be of type '{ expectedType } '.",
@@ -253,34 +253,77 @@ public static void OfType(
253
253
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
254
254
[ StackTraceHidden ]
255
255
public static void OfType < TExpected > (
256
- [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
257
256
Type ? type ,
258
257
[ CallerArgumentExpression ( "type" ) ] string ? paramName = null )
259
258
{
260
259
OfType ( type , typeof ( TExpected ) , paramName ) ;
261
260
}
262
261
262
+ /// <summary>
263
+ /// Ensures that the type argument is of the expected type. Supports testing for
264
+ /// open generic types.
265
+ /// </summary>
266
+ /// <param name="type">The <see cref="Type"/> argument.</param>
267
+ /// <param name="expectedType">The expected <see cref="Type"/>.</param>
268
+ /// <param name="paramName">The parameter name.</param>
269
+ /// <exception cref="ArgumentException">The type argument is not of the expected type.</exception>
270
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
271
+ [ StackTraceHidden ]
272
+ public static void OfGenericType (
273
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
274
+ Type ? type ,
275
+ Type expectedType ,
276
+ [ CallerArgumentExpression ( "type" ) ] string ? paramName = null )
277
+ {
278
+ Ensure . Arg . NotNull ( expectedType ) ;
279
+
280
+ if ( type != null && ! IsAssignableTo ( type , expectedType ) )
281
+ {
282
+ throw new ArgumentException (
283
+ $ "Argument '{ paramName } ' is of type '{ type } ' but expected to be of type '{ expectedType } '.",
284
+ paramName ) ;
285
+ }
286
+ }
287
+
288
+ /// <summary>
289
+ /// Ensures that the type argument is of the expected type. Supports testing for
290
+ /// open generic types.
291
+ /// </summary>
292
+ /// <typeparam name="TExpected">The expected <see cref="Type"/>.</typeparam>
293
+ /// <param name="type">The <see cref="Type"/> argument.</param>
294
+ /// <param name="paramName">The parameter name.</param>
295
+ /// <exception cref="ArgumentException">The type argument is not of the expected type.</exception>
296
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
297
+ [ StackTraceHidden ]
298
+ public static void OfGenericType < TExpected > (
299
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
300
+ Type ? type ,
301
+ [ CallerArgumentExpression ( "type" ) ] string ? paramName = null )
302
+ {
303
+ OfGenericType ( type , typeof ( TExpected ) , paramName ) ;
304
+ }
305
+
263
306
[ StackTraceHidden ]
264
307
private static bool IsAssignableTo (
265
308
[ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
266
309
Type ? givenType ,
267
- Type ? genericType )
310
+ Type expectedType )
268
311
{
269
- if ( givenType == null || genericType == null )
312
+ if ( givenType == null || expectedType == null )
270
313
{
271
314
return false ;
272
315
}
273
316
274
- if ( genericType . GetTypeInfo ( )
317
+ if ( expectedType . GetTypeInfo ( )
275
318
. IsAssignableFrom ( givenType . GetTypeInfo ( ) ) )
276
319
{
277
320
return true ;
278
321
}
279
322
280
- return givenType == genericType
281
- || MapsToGenericTypeDefinition ( givenType , genericType )
282
- || HasInterfaceThatMapsToGenericTypeDefinition ( givenType , genericType )
283
- || IsAssignableTo ( givenType . GetTypeInfo ( ) . BaseType , genericType ) ;
323
+ return givenType == expectedType
324
+ || MapsToGenericTypeDefinition ( givenType , expectedType )
325
+ || HasInterfaceThatMapsToGenericTypeDefinition ( givenType , expectedType )
326
+ || IsAssignableTo ( givenType . GetTypeInfo ( ) . BaseType , expectedType ) ;
284
327
}
285
328
286
329
[ StackTraceHidden ]
0 commit comments