1
這是我寫的測試我的應用程序的app.config文件的值,我想知道這是一個好方法嗎?我直接在myProperty的getter/setter方法拋出一個ArgumentOutOfRangeException:它是一個很好的方式來測試ConfigurationElement屬性獲取/設置值
internal sealed class ProcessingMyPropertyElement : ConfigurationElement
{
[ConfigurationProperty("myproperty", IsRequired = true)]
public int MyProperty
{
get
{
if ((int)this["myproperty"] < 0 || (int)this["myproperty"] > 999)
throw new ArgumentOutOfRangeException("myproperty");
return (int)this["myproperty"];
}
set
{
if (value < 0 || value > 999)
throw new ArgumentOutOfRangeException("myproperty");
this["recurEvery"] = value;
}
}
}
聽起來很好。在getter中,我會添加一個int.TryParse以檢測配置文件中的非int值 – 2011-12-20 09:50:18