У меня есть объектный граф, посыпанный атрибутами DataAnnotation, где некоторые свойства объектов являются классами, которые сами имеют атрибуты проверки и т.д.
В следующем сценарии:
public class Employee
{
[Required]
public string Name { get; set; }
[Required]
public Address Address { get; set; }
}
public class Address
{
[Required]
public string Line1 { get; set; }
public string Line2 { get; set; }
[Required]
public string Town { get; set; }
[Required]
public string PostalCode { get; set; }
}
Если я попытаюсь проверить Employee
Address
без значения для PostalCode
, тогда я хотел бы (и ожидал) исключение, но я его не получаю. Вот как я это делаю:
var employee = new Employee
{
Name = "Neil Barnwell",
Address = new Address
{
Line1 = "My Road",
Town = "My Town",
PostalCode = "" // <- INVALID!
}
};
Validator.ValidateObject(employee, new ValidationContext(employee, null, null));
Какие еще параметры у меня есть с Validator
, которые гарантируют, что все свойства будут проверены рекурсивно?
Большое спасибо заранее.