2010-08-13 54 views
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屬性不正確。有什麼建議麼?

回答

1

它將該屬性解釋爲使用int重載。

它的工作有:

[Range(0.0, 5.0, ErrorMessage = "Weight must be between 0 and 5")] 
    public virtual double Weight{ get; set; }