Crazy Linq: performing System.ComponentModel.DataAnnotations validation in a single statement
Posted
by Daniel Cazzulino
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by Daniel Cazzulino
Published on Thu, 15 Apr 2010 17:08:26 GMT
Indexed on
2010/04/15
17:13 UTC
Read the original article
Hit count: 451
.NET
|All Technology
public static IEnumerable<ValidationResult> Validate(object component)
{
return from descriptor in TypeDescriptor.GetProperties(component).Cast<PropertyDescriptor>()
from validation in descriptor.Attributes.OfType<System.ComponentModel.DataAnnotations.ValidationAttribute>()
where !validation.IsValid(descriptor.GetValue(component))
select new ValidationResult(
validation.ErrorMessage ?? string.Format(CultureInfo.CurrentUICulture, "{0} validation failed.", validation.GetType().Name),
new[] { descriptor.Name });
}
...
© ASP.net Weblogs or respective owner