11

我正在使用 app.config 文件来存储凭据,当我尝试检索它们时,我得到TypeLoadException如下:

无法从程序集“System.Configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“System.Configuration.DictionarySectionHandler”

这是一个 .NET 4.5 项目,我将SystemandSystem.Configuration Copy-Local属性设置为true,但我不明白问题出在哪里。我没有 .NET 编程经验,因此对汇编的概念不太了解。

以下是代码片段:

应用程序配置

<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");

我知道这是一个常见的问题,我在发布之前搜索了它。但是到目前为止我发现的所有解决方案似乎都不起作用,或者我可能没有正确理解它们。

先感谢您。

4

1 回答 1

3

参考msdn 文档,您需要按照 Hans 所说Fully qualified class name的那样使用。app.config在你的代码中它会是

<sectionGroup name="Credentials">
   <section name="Twitter" type="System.Configuration.DictionarySectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</sectionGroup>
于 2017-01-22T19:46:55.477 回答