Not from me, except my friend Steve Sanderson:
internal static class DataAnnotationsValidationRunner { public static IEnumerable<ErrorInfo> GetErrors(object instance) { return from prop in TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>() from attribute in prop.Attributes.OfType<ValidationAttribute>() where !attribute.IsValid(prop.GetValue(instance)) select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance); } }
You may need to improve this, for example, if you want [DataType (DataType.EmailAddress)] to actually check email addresses or want to support the [MetadataType] attribute.
source share