我有通过构造函数设置私有属性的类。
然后我想从基类运行以下代码来检查传递的值是否正常: ValidationContext context = new ValidationContext(this,null,null);
ValidationResults = new 列表();
if (!System.ComponentModel.DataAnnotations.Validator.TryValidateObject(this, context, ValidationResults, true)) { this.Success = false; this.StatusCode = CommandStatusCode.ValidationFailed; return false; } return true;
问题是,TryValidateObject仅验证公共属性。有没有办法验证私人财产?
答案 0 :(得分:0)
我会在构造函数的参数上使用代码契约。