我有一个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
。
显然,自定义部分组可以设置得更好但我无法修复,所以我想知道:是否有可能让Configuration
对象保存,如果它有自定义sectionGroups NameValueSectionHandler
类型