1
是工作使用.net 3.5范围DataAnnotation似乎并不在.net 3.5
我有一系列的特性上的属性(System.ComponentModel.DataAnnotations)...
[Range(0, 5, ErrorMessage = "Weight must be between 0 and 5")]
public virtual double Weight{ get; set; }
而且我有一个检查验证属性类中的验证方法...
protected virtual void Validate()
{
var type = this.GetType();
foreach (var property in type.GetProperties())
{
foreach (ValidationAttribute attribute in
property.GetCustomAttributes(typeof(ValidationAttribute),true))
{
if(!attribute.IsValid(property.GetValue(this, null)))
{
BrokenRules.Add(attribute.ErrorMessage);
}
}
}
}
public virtual bool IsValid()
{
return GetBrokenRules().Count == 0;
}
而且我有一个NUnit的测试,测试验证...
[TestCase(-.1, Result = false)] // fails
[TestCase(0.0, Result = true)]
[TestCase(5.0, Result = true)]
[TestCase(5.1, Result = false)] // fails
public bool ItValidatesWeight(double weight)
{
_ornament.Weight = weight;
return _ornament.IsValid();
}
必需的属性正常工作,但在类上并正确测试,但Range属性不正确。有什么建议么?