我有一個包含一個類中的下列ConfigurationSection:我可以在自定義ConfigurationSection上使用IntegerValidator屬性指定範圍嗎?
namespace DummyConsole {
class TestingComponentSettings: ConfigurationSection {
[ConfigurationProperty("waitForTimeSeconds", IsRequired=true)]
[IntegerValidator(MinValue = 1, MaxValue = 100, ExcludeRange = false)]
public int WaitForTimeSeconds
{
get { return (int)this["waitForTimeSeconds"]; }
set { this["waitForTimeSeconds"] = value; }
}
[ConfigurationProperty("loginPage", IsRequired = true, IsKey=false)]
public string LoginPage
{
get { return (string)this["loginPage"]; }
set { this["loginPage"] = value; }
}
}
}
然後我在我的config文件如下:
<configSections>
<section name="TestingComponentSettings"
type="DummyConsole.TestingComponentSettings, DummyConsole"/>
</configSections>
<TestingComponentSettings waitForTimeSeconds="20" loginPage="myPage" />
當我再嘗試使用此配置節中,我得到以下錯誤:
var Testing = ConfigurationManager.GetSection("TestingComponentSettings")
as TestingComponentSettings;
ConfigurationErrorsException was unhandled
The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must be inside the range 1-100.
如果我陳GE的IntegerValidator
有一個ExcludeRage =真,我(顯然)獲得:
ConfigurationErrorsException was unhandled
The value for the property 'waitForTimeSeconds' is not valid. The error is: The value must not be in the range 1-100
如果我再改在.config到數高於100的屬性的值,它的工作原理。
如果我將驗證器更改爲只有一個MaxValue
爲100,它可以工作,但也會接受值-1。
有沒有可能像這樣使用IntegerValidatorAttribute
?
編輯補充
微軟鏈接今天已經更新了一個解決方案。顯然,如果沒有指定默認值,它將使用「0」作爲默認值。當然,0在1-100的範圍之外。 「解決方案」是將DefaultValue =參數添加到ConfigurationProperty屬性,並使用默認值在該範圍內。不幸的是,這意味着你正在強加一個默認值,這可能不是你想要的或者需要的。 我一直有這個問題了。很高興我偶然發現了這個問題! – Skrud 2010-01-27 21:12:09