ConfigurationValidatorBase验证方法接收默认值
本文关键字:默认值 方法 验证 ConfigurationValidatorBase | 更新日期: 2023-09-27 18:05:47
我试图建立一个FloatValidatorAttribute。在这篇msdn文章中:https://msdn.microsoft.com/en-us/library/system.configuration.configurationvalidatorattribute(v=vs.110).aspx
有一些例子。"ProgrammableValidator"和它的属性示例就是我想要的浮点验证器。
我能在这个网站上找到的唯一相关的东西是这个未回答的问题:双重使用系统的验证。配置验证器
我还发现了这个:https://social.msdn.microsoft.com/Forums/vstudio/en-US/6faf9c70-162c-499b-8d0c-0b1f19c7a24a/issues-with-custom-configuration-validator-and-attribute?forum=clr那个人和我有类似的问题。但这对我没有帮助
我的问题是来自网络的价值。配置没有正确传递给我创建的FloatValidator的Validate方法。
下面是我的代码:class FloatValidator : ConfigurationValidatorBase
{
public float MinValue { get; private set; }
public float MaxValue { get; private set; }
public FloatValidator(float minValue, float maxValue)
{
MinValue = minValue;
MaxValue = maxValue;
}
public override bool CanValidate(Type type)
{
return type == typeof(float);
}
public override void Validate(object obj)
{
float value;
try
{
value = float.Parse(obj.ToString());
}
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}");
}
}
}
属性self:
class FloatValidatorAttribute : ConfigurationValidatorAttribute
{
public float MinValue { get; set; }
public float MaxValue { get; set; }
public FloatValidatorAttribute(float minValue, float maxValue)
{
MinValue = minValue;
MaxValue = maxValue;
}
public override ConfigurationValidatorBase ValidatorInstance => new FloatValidator(MinValue, MaxValue);
}
配置元素self:
public class Compound : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name => this["name"] as string;
[ConfigurationProperty("abbreviation", IsRequired = true)]
public string Abbreviation => this["abbreviation"] as string;
[ConfigurationProperty("id", IsRequired = true)]
[IntegerValidator(ExcludeRange = false, MinValue = 0, MaxValue = int.MaxValue)]
public int Id => (int)this["id"];
[ConfigurationProperty("factor", IsRequired = true)]
[FloatValidator(float.Epsilon, float.MaxValue)]
public float Factor => (float) this["factor"];
}
下面是一个来自web.config
的复合元素示例 <add name="Ozone" abbreviation="O3" id="147" factor="1.9957"/>
<add name="Particles smaller than 10 µm, Tapered Element Oscillating Microbalance measurement" abbreviation="PM10Teom" id="161" factor="1" />
我可以正确地检索值,并且我可以将因子应用于我正在处理的测量。但是如果我应用FloatValidator,传递给类FloatValidator()的所有值都是0,所以我不能实际验证输入。
提前感谢
框架似乎正在验证属性的默认值。由于没有默认值,所以使用default(float)
。这就是为什么你在传递0的地方看到一个Validate调用。
由于验证失败,因此看不到后续调用。它们将包括您配置中的相关值。
您应该为Factor
提供一个默认值:
[ConfigurationProperty("factor", IsRequired = true, DefaultValue = float.Epsilon)]
用于Id
-属性的内置IntegerValidator
-属性实际上也是如此。如果您使用的范围不包含零,并且不应用默认值,则将无法验证。参见https://stackoverflow.com/a/2150643/1668425。
似乎可以。
使用这个app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CompoundConfiguration" type="ConsoleApplication2.CompoundConfigurationSection,ConsoleApplication2,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<CompoundConfiguration>
<Compounds>
<add name="Particles smaller than 10 µm, Tapered Element Oscillating Microbalance measurement" abbreviation="PM10Teom" id="161" factor="1" />
<add name="Ozone" abbreviation="O3" id="147" factor="1.9957" />
</Compounds>
</CompoundConfiguration>
</configuration>
并提供ConfigurationSection的实现:
public class CompoundConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Compounds", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(CompoundCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public CompoundCollection Compounds
{
get
{
return (CompoundCollection)base["Compounds"];
}
}
}
和一个ElementCollection:
public class CompoundCollection : ConfigurationElementCollection
{
public CompoundCollection()
{
}
public Compound this[int index]
{
get { return (Compound)BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public void Add(Compound serviceConfig)
{
BaseAdd(serviceConfig);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new Compound();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Compound)element).Id;
}
public void Remove(Compound serviceConfig)
{
BaseRemove(serviceConfig.Id);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
}
运行这个main:
static void Main(string[] args)
{
var compounds = ConfigurationManager.GetSection("CompoundConfiguration");
}
给出一个异常,消息为:
Value too low, minimum value allowed: 1,401298E-45
我猜哪个是预期的结果?