我正在使用 app.config 文件来存储凭据,当我尝试检索它们时,我得到了一个 TypeLoadException
,如下所示:
Could not load type 'System.Configuration.DictionarySectionHandler' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
这是一个 .NET 4.5 项目,我将 System
和 System.Configuration
Copy-Local
属性设置为 true
,我不明白问题出在哪里。我没有 .NET 编程经验,所以对汇编的概念不太熟悉。
以下是代码片段:
app.config
<configSections>
<sectionGroup name="Credentials">
<section name="Twitter" type="System.Configuration.DictionarySectionHandler"/>
</sectionGroup>
</configSections>
<Credentials>
<Twitter>
<add key="****" value="*****"/>
<add key="****" value="*****"/>
</Twitter>
</Credentials>
连接服务文件
var hashtable = (Hashtable)ConfigurationManager.GetSection("Credentials/Twitter");
我知道这是一个常见问题,我在发帖前用谷歌搜索了一下。但是到目前为止我找到的所有解决方案似乎都不起作用,或者我可能没有正确理解它们。
提前谢谢你。
最佳答案
引用msdn documentation正如 Hans 所说,您需要在 app.config
中使用 Fully qualified class name
。在您的代码中它将是
<sectionGroup name="Credentials">
<section name="Twitter" type="System.Configuration.DictionarySectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</sectionGroup>
关于c# - 类型加载异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21826982/