@@ -372,9 +372,8 @@ private static async Task<bool> TryValidateImpl(
372372 }
373373
374374 // Once we get to this point we have to box the target in order to track whether we've validated it or not
375- if ( validatedObjects . ContainsKey ( target ) )
375+ if ( validatedObjects . TryGetValue ( target , out var result ) )
376376 {
377- var result = validatedObjects [ target ] ;
378377 // If there's a null result it means this object is the one currently being validated
379378 // so just skip this reference to it by returning true. If there is a result it means
380379 // we already validated this object as part of this validation operation.
@@ -503,10 +502,8 @@ private static async Task<bool> TryValidateImpl(
503502 }
504503 }
505504
506- if ( isValid && typeof ( IValidatableObject ) . IsAssignableFrom ( targetType ) )
505+ if ( isValid && target is IValidatableObject validatable )
507506 {
508- var validatable = ( IValidatableObject ) target ;
509-
510507 // Reset validation context
511508 validationContext . MemberName = null ;
512509 validationContext . DisplayName = validationContext . ObjectType . Name ;
@@ -519,15 +516,13 @@ private static async Task<bool> TryValidateImpl(
519516 }
520517 }
521518
522- if ( isValid && typeof ( IAsyncValidatableObject ) . IsAssignableFrom ( targetType ) )
523- {
524- var validatable = ( IAsyncValidatableObject ) target ;
525-
519+ if ( isValid && target is IAsyncValidatableObject asyncValidatable )
520+ {
526521 // Reset validation context
527522 validationContext . MemberName = null ;
528523 validationContext . DisplayName = validationContext . ObjectType . Name ;
529524
530- var validateTask = validatable . ValidateAsync ( validationContext ) ;
525+ var validateTask = asyncValidatable . ValidateAsync ( validationContext ) ;
531526 ThrowIfAsyncNotAllowed ( validateTask . IsCompleted , allowAsync ) ;
532527
533528 var validatableResults = await validateTask . ConfigureAwait ( false ) ;
0 commit comments