Tag: configurationelement

使用.Net配置框架加载具有所需子ConfigurationElement的ConfigurationSection

我有一个控制台应用程序试图从web.config文件加载CustomConfigurationSection。 自定义配置部分具有所需的自定义配置元素。 这意味着当我加载配置部分时,如果配置中不存在该配置元素,我希望看到exception。 问题是.NET框架似乎完全忽略了isRequired属性。 因此,当我加载配置部分时,我只创建自定义配置元素的实例并将其设置在配置部分。 我的问题是,为什么会发生这种情况? 我希望GetSection()方法触发ConfigurationErrorsexception,因为配置中缺少必需的元素。 这是我的配置部分的外观。 public class MyConfigSection : ConfigurationSection { [ConfigurationProperty(“MyConfigElement”, IsRequired = true)] public MyConfigElement MyElement { get { return (MyConfigElement) this[“MyConfigElement”]; } } } public class MyConfigElement : ConfigurationElement { [ConfigurationProperty(“MyAttribute”, IsRequired = true)] public string MyAttribute { get { return this[“MyAttribute”].ToString(); } } } 这是我加载配置部分的方法。 class Program { […]