我正在创建一个自定义配置部分(继承 System.Configuration.ConfigurationSection),我想知道是否必须对一个可为 Nullable int 的 ConfigurationProperty 进行值验证。即,我是否必须这样做:
[ConfigurationProperty("NullableInt", IsRequired = true)]
public int? NullableInt
{
get
{
return String.IsNullOrEmpty(Convert.ToString(this["NullableInt"]))
? (int?) null
: Convert.ToInt32(this["NullableInt"]);
}
set
{
this["NullableInt"] = value.HasValue ? Convert.ToString(value) : "";
}
}
或者我可以做这样的事情:
[ConfigurationProperty("NullableInt", IsRequired = true)]
public int? NullableInt
{
get{ return Convert.ToInt32(this["NullableInt"]); }
set{ this["NullableInt"] = Convert.ToString(value); }
}
或者有没有更好的方法?
提前致谢。