范围DataAnnotation似乎并不奏效在.NET 3.5中 [英] Range DataAnnotation Doesn't Seem to Be Working in .Net 3.5

查看:110
本文介绍了范围DataAnnotation似乎并不奏效在.NET 3.5中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.net 3.5

Using .Net 3.5

我有一系列的特性(System.ComponentModel.DataAnnotations)上的属性...

I have a Range Attributes (System.ComponentModel.DataAnnotations) on a Property...

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

和我有一个验证方法,在课堂上,检查验证属性...

And I have a Validate method in the class that checks validation attributes...

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测试,测试验证...

And I have an NUnit test that tests the validation...

[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();
}

必需的属性是否工作正常,但在类和正确的测试,但幅度属性都没有。有什么建议?

Required attributes are working properly but on the class and test correctly, but the Range attributes are not. Any suggestions?

推荐答案

这是除preting属性作为使用INT超载。

It was interpreting the attribute as using the int overload.

它的工作有:

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

这篇关于范围DataAnnotation似乎并不奏效在.NET 3.5中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆