给定以下对象,
public class Question { [required] public string QuestionText { get; set; } [Range(1,5)] public int Difficulty { get; set; } }
具有以下验证代码
ICollection<ValidationResult> results = new List<ValidationResult>(); Question question = new Question(); ValidationContext ctx = new ValidationContext(question,null,null); Validator.TryValidateObject(question,ctx,results); // results.Length = 1