这是测试 ConfigurationElement 的属性 getters/setters 中的值的好方法吗
这是我编写的用于测试应用程序的 app.config 文件值的内容,我想知道这是否是一个好方法?我直接在 MyProperty getter/setter 中抛出 ArgumentOutOfRangeException:
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;
}
}
}
Here is what I've written to test values of the app.config file of my application and I want to know if it's a good way ? I throw an ArgumentOutOfRangeException directly in the MyProperty getters/setters:
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置值时,最好检查范围并在无效时抛出异常。
得到它后,我不会抛出异常。这样,有人就可以通过手动编辑 app.config 来使应用程序崩溃。在你的 getter 中,我会将值限制在特定范围内并返回有效结果。
When setting the value it is a good idea to check for the range and throw an exception when this is invalid.
On getting it, I wouldn't throw an exception. This way someone could crash the application by editing the app.config by hand. In your getter I would limit the value to the specific range and return a valid result.