0
我想能够验证使用DataAnnotations在.NET 4上下文依赖的验证
例如不同的上下文中的对象:如果我有一类具有这些注释属性
[Required]
public string Name { get; set; }
[Required]
public string PhoneNumber { get; set; }
[Required]
public string Address { get; set; }
我希望能够像做
bool namePhoneValid = Validator.TryValidateObject(entity, contextNamePhone, results1);
bool allValid = Validator.TryValidateObject(entity, contextAll, results2);
其中contextNamePhone只验证姓名和电话,并contextAll验证所有属性(姓名,电话和地址在这种情况下) 。
这可能吗?应该如何构建验证上下文?有其他/更聪明的方法来做到这一点?