【问题标题】:ConfigurationSection vs IConfigurationSectionHandlerConfigurationSection 与 IConfigurationSectionHandler
【发布时间】:2016-02-19 11:46:12
【问题描述】:

我想在我的配置中使用自定义元素。

在编写我的自定义配置类时,我使用了 ConfigurationSection,但是当我的代码尝试从我的自定义配置元素中访问信息时,它会抛出一个错误,指出:

为 XxxSection 创建配置节处理程序时出错:类型“Xxx”不继承自“System.Configuration.IConfigurationSectionHandler”。

我的印象是 IConfigurationSectionHandler 在 .NET Framework 2.0 中已被贬低,因此想知道如果我的代码使用 .NET Framework 4.0,在 Windows 8.1 上通过 VS2013 进行调试,是否有任何情况会导致引发该错误64位机器。

我读过的大多数讨论这些类的线程都有几年的历史了,并且大约在 IConfigurationSectionHandler 的贬值仍在发生的时候(在某些地方有说明,但没有在文档等处说明),因此围绕它存在混淆。

如果可以的话,我想避免重新使用已折旧的接口。谢谢

【问题讨论】:

    标签: c# .net configuration


    【解决方案1】:

    我已经设法找出问题所在,配置文件顶部的 configSection 设置不正确。该类型应始终引用从 ConfigurationSection 继承的类:

    <configuration>
      <configSections>
        <section name="purgeDirectorySection" type="Foo.FooBarSection, Foo"/>
      </configSections>
      <appSettings>
        ...
      </appSettings>
      <FooBarSection>
        ...
      </FooBarSection>
    </configuration>
    

    现在这对我有用。希望这对可能遇到此问题的其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      相关资源
      最近更新 更多