使用System.Configuration验证程序进行双重验证 [英] Validation of double using System.Configuration validator

查看:72
本文介绍了使用System.Configuration验证程序进行双重验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为我的库创建一个自定义配置部分.配置中的参数之一应为正双精度.但是,由于某种原因,我找不到用于Double的任何Validator.

I had to create a custom configuration section for my library. One of the parameters in the configuration should be positive double. However, for some reason I could not find any Validator for double.

例如,这是我使用整数验证器的方式:

For instance, here's how I use the integer validator:

[ConfigurationProperty("someProp", IsRequired = false, DefaultValue = 15)]
[IntegerValidator(MaxValue = 200, MinValue = 0)]
public int SomeProperty
{
   get
   {
      return (int)this["someProp"];
   }
   set
   {
      this["someProp"] = value;
   }
}

我浏览了Google,却找不到DoubleValidator或FloatValidator的任何提及.是否有可能以某种方式确保该物业为正双房?是否有任何原因DoubleValidator在System.Configuration中不存在?也许我弄错了,并且双重属性不应该存储在配置文件中.

I've looked through Google and was not able to find any mention of DoubleValidator or FloatValidator. Is it possible to somehow make sure the property is a positive double? Are there any reasons DoubleValidator is not present in the System.Configuration? Maybe I am mistaken and double properties should not be stored in the config file.

推荐答案

我需要为自己的项目使用float验证程序,并找到了您的问题. 这就是我制作验证器的方式.当/如果使用它,应该记住在用于注释属性的ConfigurationPropertyAttribute上设置默认值.

I needed a float validator for my own project, and found your question. This is how I made my validator. When/if you use it, you should remember to set a default value on the ConfigurationPropertyAttribute that you annotate the property with.

感谢KasperVidebæk发现我的错误: ConfigurationValidatorBase验证方法会接收默认值

Thanks to Kasper Videbæk for finding my mistake: ConfigurationValidatorBase validate method receives default value

class DoubleValidator : ConfigurationValidatorBase
{
    public double MinValue { get; private set; }
    public double MaxValue { get; private set; }

    public DoubleValidator(double minValue, double maxValue)
    {
        MinValue = minValue;
        MaxValue = maxValue;
    }

    public override bool CanValidate(Type type)
    {
        return type == typeof(double);
    }

    public override void Validate(object obj)
    {
        double value;
        try
        {
            value = Convert.ToDouble(obj);
        }
        catch (Exception)
        {
            throw new ArgumentException();
        }

        if (value < MinValue)
        {
            throw new ConfigurationErrorsException($"Value too low, minimum value allowed: {MinValue}");
        }

        if (value > MaxValue)
        {
            throw new ConfigurationErrorsException($"Value too high, maximum value allowed: {MaxValue}");
        }
    }
}

要在配置属性上使用的属性

The attribute to use on the configurationproperty

class DoubleValidatorAttribute : ConfigurationValidatorAttribute
{
    public double MinValue { get; set; }
    public double MaxValue { get; set; }

    public DoubleValidatorAttribute(double minValue, double maxValue)
    {
        MinValue = minValue;
        MaxValue = maxValue;
    }

    public override ConfigurationValidatorBase ValidatorInstance => new DoubleValidator(MinValue, MaxValue);
}

这篇关于使用System.Configuration验证程序进行双重验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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