2010-08-13 3 views
1

에서 제대로 동작 ...범위 DataAnnotation 닷넷 3.5 나는 범위가 속성에 (System.ComponentModel.DataAnnotations)를 속성이</p> <p>를 사용하여 닷넷 3.5

[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; } 
:

그것은 함께 일

관련 문제