@@ -220,8 +220,7 @@ public static void MinLength(
220220 }
221221
222222 /// <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.
225224 /// </summary>
226225 /// <param name="type">The <see cref="Type"/> argument.</param>
227226 /// <param name="expectedType">The expected <see cref="Type"/>.</param>
@@ -230,12 +229,13 @@ public static void MinLength(
230229 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
231230 [ StackTraceHidden ]
232231 public static void OfType (
233- [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
234232 Type ? type ,
235- Type ? expectedType ,
233+ Type expectedType ,
236234 [ CallerArgumentExpression ( "type" ) ] string ? paramName = null )
237235 {
238- if ( ! IsAssignableTo ( type , expectedType ) )
236+ Ensure . Arg . NotNull ( expectedType ) ;
237+
238+ if ( type != null && ! expectedType . IsAssignableFrom ( type ) )
239239 {
240240 throw new ArgumentException (
241241 $ "Argument '{ paramName } ' is of type '{ type } ' but expected to be of type '{ expectedType } '.",
@@ -253,34 +253,77 @@ public static void OfType(
253253 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
254254 [ StackTraceHidden ]
255255 public static void OfType < TExpected > (
256- [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
257256 Type ? type ,
258257 [ CallerArgumentExpression ( "type" ) ] string ? paramName = null )
259258 {
260259 OfType ( type , typeof ( TExpected ) , paramName ) ;
261260 }
262261
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+
263306 [ StackTraceHidden ]
264307 private static bool IsAssignableTo (
265308 [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
266309 Type ? givenType ,
267- Type ? genericType )
310+ Type expectedType )
268311 {
269- if ( givenType == null || genericType == null )
312+ if ( givenType == null || expectedType == null )
270313 {
271314 return false ;
272315 }
273316
274- if ( genericType . GetTypeInfo ( )
317+ if ( expectedType . GetTypeInfo ( )
275318 . IsAssignableFrom ( givenType . GetTypeInfo ( ) ) )
276319 {
277320 return true ;
278321 }
279322
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 ) ;
284327 }
285328
286329 [ StackTraceHidden ]
0 commit comments