代码之家  ›  专栏  ›  技术社区  ›  Allen Rice 0x6A75616E

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

  •  1
  • Allen Rice 0x6A75616E  · 技术社区  · 14 年前

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

    sectionGroup System.Configuration.NameValueSectionHandler (我没有写那部分,我不能改变它)。现在当我打电话给 Save 方法,它抛出一个 TypeLoadException

    信息:

    类型 不继承自 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>
    

    保存 很好。

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

    0 回复  |  直到 14 年前