为什么这总是返回true?
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Age = 24;
ICollection<ValidationResult> results = new Collection<ValidationResult>();
bool isValid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), results);
Console.WriteLine("Valid = {0}",isValid);
foreach (var result in results)
{
Console.WriteLine(result.ErrorMessage);
}
Console.ReadKey();
}
}
public class Person
{
[Required(ErrorMessage = "You have to identify yourself!!")]
public int Id { get; set; }
public decimal Age { get; set; }
}
我的用法有什么问题?