使用 NameValueSectionHandler 类型的自定义节组对配置对象调用 Save
我有一个 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 fromSystem.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论