66
77using System ;
88using System . Collections ;
9+ using System . Collections . Generic ;
910using System . ComponentModel ;
1011using System . Linq ;
1112using System . Reactive . Concurrency ;
@@ -24,6 +25,7 @@ namespace ReactiveUI.Validation.Helpers
2425 public abstract class ReactiveValidationObject < TViewModel > : ReactiveObject , IValidatableViewModel , INotifyDataErrorInfo
2526 {
2627 private readonly ObservableAsPropertyHelper < bool > _hasErrors ;
28+ private readonly Dictionary < string , string > _propertyMemberNameDictionary = new Dictionary < string , string > ( ) ;
2729
2830 /// <summary>
2931 /// Initializes a new instance of the <see cref="ReactiveValidationObject{TViewModel}"/> class.
@@ -60,25 +62,38 @@ protected ReactiveValidationObject(IScheduler scheduler = null)
6062 /// <inheritdoc />
6163 public virtual IEnumerable GetErrors ( string propertyName )
6264 {
63- var memberInfoName = GetType ( )
64- . GetMember ( propertyName )
65- . FirstOrDefault ( ) ?
66- . ToString ( ) ;
65+ return string . IsNullOrEmpty ( propertyName ) ?
66+ SelectValidations ( )
67+ . SelectMany ( validation => validation . Text )
68+ . ToArray ( ) :
69+ GetMemberInfoName ( propertyName ) is string memberInfoName && string . IsNullOrEmpty ( memberInfoName ) == false ?
70+ SelectValidations ( )
71+ . Where ( validation => validation . ContainsPropertyName ( memberInfoName ) )
72+ . SelectMany ( validation => validation . Text )
73+ . ToArray ( ) :
74+ Enumerable . Empty < string > ( ) ;
75+
76+ IEnumerable < IPropertyValidationComponent < TViewModel > > SelectValidations ( ) =>
77+
78+ ValidationContext
79+ . Validations
80+ . OfType < IPropertyValidationComponent < TViewModel > > ( )
81+ . Where ( validation => ! validation . IsValid ) ;
82+ }
6783
68- if ( memberInfoName == null )
84+ private string GetMemberInfoName ( string propertyName )
85+ {
86+ if ( _propertyMemberNameDictionary . ContainsKey ( propertyName ) == false )
6987 {
70- return Enumerable . Empty < string > ( ) ;
88+ _propertyMemberNameDictionary . Add ( propertyName , GetMemberInfoName ( ) ) ;
7189 }
7290
73- var relatedPropertyValidations = ValidationContext
74- . Validations
75- . OfType < IPropertyValidationComponent < TViewModel > > ( )
76- . Where ( validation => validation . ContainsPropertyName ( memberInfoName ) ) ;
91+ return _propertyMemberNameDictionary [ propertyName ] ;
7792
78- return relatedPropertyValidations
79- . Where ( validation => ! validation . IsValid )
80- . SelectMany ( validation => validation . Text )
81- . ToList ( ) ;
93+ string GetMemberInfoName ( ) => GetType ( )
94+ . GetMember ( propertyName )
95+ . FirstOrDefault ( ) ?
96+ . ToString ( ) ;
8297 }
8398 }
8499}
0 commit comments