c# - 类型加载异常

标签 c# exception .net-assembly system.configuration

我正在使用 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 项目,我将 SystemSystem.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/

相关文章:

python - PyQt - 如何处理模块导入异常

c# - 如何将此程序集加载到我的 AppDomain 中?

c# - 即使在部署后也从不同的文件夹引用 dll

c# - 为什么在内部类上 C# 动态找不到接口(interface)的方法?

c# - 使用属性向方法添加参数

php - 我什么时候编写自己的异常类?

c# - 无需解包即可从存档中获取程序集文件版本

c# - NpGsql Entity Framework 6 - "An operation is already in progress"

c# - 我如何获得屏幕方向?

java - 返回 null 或抛出异常