Posted by Eric Polerecky in ASP.NET MVC, DataAnnotations, ModelBinders, TDDNov 26th, 2009 | No Comments
public static IList<String> GetClassLevelErrors(object instance){ return TypeDescriptor.GetAttributes(instance).OfType<ValidationAttribute>() .Where(attribute => !attribute.IsValid(instance)) .Select(attribute => attribute.FormatErrorMessage(string.Empty)) .ToList();}
[TestMethod]public void TestMethod1(){ var prop = typeof (DataAnnotationsModelBinderSpike.Models.Contact).GetProperty("First"); var attrib = prop.GetCustomAttributes(true).Cast<RequiredAttribute>().FirstOrDefault(); Assert.IsNotNull(attrib);}