ConfigurationSection 与 IConfigurationSectionHandler

ConfigurationSection vs IConfigurationSectionHandler

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

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

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

我的印象是 IConfigurationSectionHandler 在 .NET Framework 2.0 中已经贬值,所以想知道如果我的代码使用 .NET Framework 4.0,是否有任何情况会导致抛出该错误,通过 VS2013 在Windows 8.1 64 位机器。

我读过的大多数讨论这些问题的帖子 类 已经有几年的历史了,并且是在 IConfigurationSectionHandler 的贬值仍在发生的时候(在某些地方有说明,但没有文档等)所以周围有混乱。

如果可以的话,我想避免恢复使用折旧接口。谢谢

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

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

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