使用 NameValueSectionHandler 类型的自定义节组对配置对象调用 Save

发布于 2024-08-26 06:37:26 字数 3382 浏览 11 评论 0原文

我有一个 Configuration 对象,我可以轻松地从中读取和写入设置并调用 Save(ConfigurationSaveMode.Minimal, true) 通常工作得很好。

但是,我现在有一个配置文件,其中定义了 System.Configuration.NameValueSectionHandler (我没有写那部分,我无法更改它)。现在,当我调用 保存< /a> 方法,它会抛出 TypeLoadException消息

类型 System.Configuration.NameValueSectionHandler 不继承自 System.Configuration.ConfigurationSectionGroup

调用堆栈:

at System.Configuration.TypeUtil.VerifyAssignableType
      (Type baseType, Type type, Boolean throwOnError)
at System.Configuration.TypeUtil.GetConstructorWithReflectionPermission
      (Type type, Type baseType, Boolean throwOnError)
at System.Configuration.MgmtConfigurationRecord.CreateSectionGroupFactory
      (FactoryRecord factoryRecord)
at System.Configuration.MgmtConfigurationRecord.EnsureSectionGroupFactory
      (FactoryRecord factoryRecord)
at System.Configuration.MgmtConfigurationRecord.GetSectionGroup
      (String configKey)
at System.Configuration.ConfigurationSectionGroupCollection.Get
      (String name)
at System.Configuration.ConfigurationSectionGroupCollection.
      <GetEnumerator>d__0.MoveNext()
at System.Configuration.Configuration.ForceGroupsRecursive
      (ConfigurationSectionGroup group)
at System.Configuration.Configuration.SaveAsImpl
      (String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at System.Configuration.Configuration.Save
      (ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at MyCompany.MyProduct.blah.blah.Save
      () in C:\\projects\\blah\\blah\\blah.cs:line blah

仅供参考,配置如下所示

<configuration>
  <configSections>
    ... normal sections here that work just fine, followed by ...

    <sectionGroup name="threadSettings" type="System.Configuration.NameValueSectionHandler">
      <section name="T1" type="System.Configuration.DictionarySectionHandler"/>
      <section name="T2" type="System.Configuration.DictionarySectionHandler"/>
      <section name="T3" type="System.Configuration.DictionarySectionHandler"/>
    </sectionGroup>
  </configSections>
  <applicationSettings />
  <threadSettings>
    <T1>
      <add key="key-here" value="value-here"/>
    </T1>
    ... T2 and T3 here as well, thats not interesting ...
  </threadSettings>
</configuration>

如果我删除实际部分和自定义部分组的定义,我就可以很好地读取/写入设置,甚至可以很好地调用Save

显然,自定义节组可以设置得更好一些,但我无法解决这个问题,所以我想知道:如果它具有自定义节组,是否可以保存Configuration对象NameValueSectionHandler 类型

I've got aConfiguration object that I can easily read and write settings from and call Save(ConfigurationSaveMode.Minimal, true) on and that typically works just fine.

However, I now have a config file that has a custom sectionGroup defined of type System.Configuration.NameValueSectionHandler (I didn't write that part and I can't change it). Now when I call the Save method, it throws a TypeLoadException

Message:

Type
System.Configuration.NameValueSectionHandler
does not inherit from
System.Configuration.ConfigurationSectionGroup.

Callstack:

at System.Configuration.TypeUtil.VerifyAssignableType
      (Type baseType, Type type, Boolean throwOnError)
at System.Configuration.TypeUtil.GetConstructorWithReflectionPermission
      (Type type, Type baseType, Boolean throwOnError)
at System.Configuration.MgmtConfigurationRecord.CreateSectionGroupFactory
      (FactoryRecord factoryRecord)
at System.Configuration.MgmtConfigurationRecord.EnsureSectionGroupFactory
      (FactoryRecord factoryRecord)
at System.Configuration.MgmtConfigurationRecord.GetSectionGroup
      (String configKey)
at System.Configuration.ConfigurationSectionGroupCollection.Get
      (String name)
at System.Configuration.ConfigurationSectionGroupCollection.
      <GetEnumerator>d__0.MoveNext()
at System.Configuration.Configuration.ForceGroupsRecursive
      (ConfigurationSectionGroup group)
at System.Configuration.Configuration.SaveAsImpl
      (String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at System.Configuration.Configuration.Save
      (ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at MyCompany.MyProduct.blah.blah.Save
      () in C:\\projects\\blah\\blah\\blah.cs:line blah

For reference, the configuration goes like so

<configuration>
  <configSections>
    ... normal sections here that work just fine, followed by ...

    <sectionGroup name="threadSettings" type="System.Configuration.NameValueSectionHandler">
      <section name="T1" type="System.Configuration.DictionarySectionHandler"/>
      <section name="T2" type="System.Configuration.DictionarySectionHandler"/>
      <section name="T3" type="System.Configuration.DictionarySectionHandler"/>
    </sectionGroup>
  </configSections>
  <applicationSettings />
  <threadSettings>
    <T1>
      <add key="key-here" value="value-here"/>
    </T1>
    ... T2 and T3 here as well, thats not interesting ...
  </threadSettings>
</configuration>

If I remove the actual sections and the definition for the custom section groups, I am able to read / write settings just fine and even call Save just fine.

Obviously the custom section groups could be setup a little nicer but I can't fix that so I'm wondering: Is it possible to get the Configuration object to save if it has custom sectionGroups of the NameValueSectionHandler type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文