public void ValidateFields <T>(T o, bool throwException, BaseService srv) { var descriptor = GetTypeDescriptor(typeof(T)); foreach (PropertyDescriptor propertyDescriptor in descriptor.GetProperties()) { foreach (var validationAttribute in propertyDescriptor.Attributes.OfType <ValidationAttribute>()) { if (validationAttribute is SFBaseValidationAttribute) { //customized by sunnet framework ValidationResult vr = validationAttribute.GetValidationResult(propertyDescriptor.GetValue(o), new ValidationContext(o, null, null)); if (vr != ValidationResult.Success) { if (throwException) { throw new Exception(vr.ErrorMessage); } BrokenRuleMessage msg = new BrokenRuleMessage(propertyDescriptor.Name, vr.ErrorMessage); var displayAtribute = propertyDescriptor.Attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (displayAtribute != null) { msg = new BrokenRuleMessage(displayAtribute.Name, vr.ErrorMessage.Replace(propertyDescriptor.Name, displayAtribute.Name)); } srv.AddBrokenRuleMessage(msg); } } else { //standard if (!validationAttribute.IsValid(propertyDescriptor.GetValue(o))) { string exceptionDescription = validationAttribute.FormatErrorMessage(propertyDescriptor.Name); if (throwException) { throw new Exception(exceptionDescription); } BrokenRuleMessage msg = new BrokenRuleMessage(propertyDescriptor.Name, exceptionDescription); var displayAtribute = propertyDescriptor.Attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (displayAtribute != null) { msg = new BrokenRuleMessage(displayAtribute.Name, exceptionDescription.Replace(propertyDescriptor.Name, displayAtribute.Name)); } srv.AddBrokenRuleMessage(msg); } } } } }