1

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

但是,我现在有一个自sectionGroup定义类型的配置文件System.Configuration.NameValueSectionHandler(我没有编写该部分,也无法更改它)。现在当我调用该Save方法时,它会抛出一个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可以正常调用。

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

4

0 回答 0