开发者

Validate private properties using ValidationAttributes in .net 4

开发者 https://www.devze.com 2023-02-24 07:51 出处:网络
I have classes which get private properties set via the constructor. I would then like to run the following code from a base class to check if the passed values are ok:

I have classes which get private properties set via the constructor.

I would then like to run the following code from a base class to check if the passed values are ok: ValidationContext context = new ValidationContext(this, n开发者_如何学Pythonull, null);

ValidationResults = new List();

if (!System.ComponentModel.DataAnnotations.Validator.TryValidateObject(this, context, ValidationResults, true))
{
    this.Success = false;
    this.StatusCode = CommandStatusCode.ValidationFailed;
    return false;
}
return true;

Problem is, the TryValidateObject only validates public properties. Is there any way of getting private properties validated?


I would use code contracts on your constructor's arguments.

0

精彩评论

暂无评论...
验证码 换一张
取 消