使用.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属性不正常。有什么建议吗?
答案 0 :(得分:1)
它将属性解释为使用int重载。
它适用于:
[Range(0.0, 5.0, ErrorMessage = "Weight must be between 0 and 5")]
public virtual double Weight{ get; set; }