public string this[string PropertyName] { get { if (!this.IsEnabled) return string.Empty; //Inspired by WAFFramework's DataErrorInfoSupport var ErrorMessages = new System.Text.StringBuilder(); var ValidationResults = new List<ValidationResult>(); if (string.IsNullOrEmpty(PropertyName)) Validator.TryValidateObject(this.Instance, new ValidationContext(this.Instance, null, null), ValidationResults); else { var Property = this.Instance.GetType().GetProperty(PropertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (Property == null || !Property.CanRead) throw new ArgumentException(PropertyName + " is not a valid property name.", PropertyName); Validator.TryValidateProperty(Property.GetValue(this.Instance, null), new ValidationContext(this.Instance, null, null) { MemberName = PropertyName }, ValidationResults); } var ErrorMessage = new System.Text.StringBuilder(); foreach (var Result in ValidationResults) ErrorMessage.AppendInNewLine(Result.ErrorMessage); return ErrorMessage.ToString(); } }
public string this[string PropertyName] { get { if (!this.IsEnabled) { return(string.Empty); } //Inspired by WAFFramework's DataErrorInfoSupport var ErrorMessages = new System.Text.StringBuilder(); var ValidationResults = new List <ValidationResult>(); if (string.IsNullOrEmpty(PropertyName)) { Validator.TryValidateObject(this.Instance, new ValidationContext(this.Instance, null, null), ValidationResults, true); } else { var Property = this.Instance.GetType().GetProperty(PropertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (Property == null || !Property.CanRead) { throw new ArgumentException(PropertyName + " is not a valid property name.", PropertyName); } Validator.TryValidateProperty(Property.GetValue(this.Instance, null), new ValidationContext(this.Instance, null, null) { MemberName = PropertyName }, ValidationResults); } var ErrorMessage = new System.Text.StringBuilder(); foreach (var Result in ValidationResults) { ErrorMessage.AppendInNewLine(Result.ErrorMessage); } return(ErrorMessage.ToString()); } }