@@ -372,9 +372,8 @@ private static async Task<bool> TryValidateImpl(
372
372
}
373
373
374
374
// 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 ) )
376
376
{
377
- var result = validatedObjects [ target ] ;
378
377
// If there's a null result it means this object is the one currently being validated
379
378
// so just skip this reference to it by returning true. If there is a result it means
380
379
// we already validated this object as part of this validation operation.
@@ -503,10 +502,8 @@ private static async Task<bool> TryValidateImpl(
503
502
}
504
503
}
505
504
506
- if ( isValid && typeof ( IValidatableObject ) . IsAssignableFrom ( targetType ) )
505
+ if ( isValid && target is IValidatableObject validatable )
507
506
{
508
- var validatable = ( IValidatableObject ) target ;
509
-
510
507
// Reset validation context
511
508
validationContext . MemberName = null ;
512
509
validationContext . DisplayName = validationContext . ObjectType . Name ;
@@ -519,15 +516,13 @@ private static async Task<bool> TryValidateImpl(
519
516
}
520
517
}
521
518
522
- if ( isValid && typeof ( IAsyncValidatableObject ) . IsAssignableFrom ( targetType ) )
523
- {
524
- var validatable = ( IAsyncValidatableObject ) target ;
525
-
519
+ if ( isValid && target is IAsyncValidatableObject asyncValidatable )
520
+ {
526
521
// Reset validation context
527
522
validationContext . MemberName = null ;
528
523
validationContext . DisplayName = validationContext . ObjectType . Name ;
529
524
530
- var validateTask = validatable . ValidateAsync ( validationContext ) ;
525
+ var validateTask = asyncValidatable . ValidateAsync ( validationContext ) ;
531
526
ThrowIfAsyncNotAllowed ( validateTask . IsCompleted , allowAsync ) ;
532
527
533
528
var validatableResults = await validateTask . ConfigureAwait ( false ) ;
0 commit comments