System.TypeLoadException:无法从程序集“ x”中加载类型“ x”吗? [英] System.TypeLoadException: Could not load type 'x' from assembly 'x'?

查看:469
本文介绍了System.TypeLoadException:无法从程序集“ x”中加载类型“ x”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对信息过多表示歉意,我只想提供任何可能相关的信息,我不确定发生了什么情况


测试方法DCIM_Test_1.MySQLDefTest.ConnectionStringTest引发了异常:
System.TypeLoadException:无法从程序集 DCIM,版本= 1.0中加载类型 DCIM_With_Test.Database.Database_MySQLDef。 0.0,Culture = neutral,PublicKeyToken = null'。

Test method DCIM_Test_1.MySQLDefTest.ConnectionStringTest threw exception: System.TypeLoadException: Could not load type 'DCIM_With_Test.Database.Database_MySQLDef' from assembly 'DCIM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

在运行其中一项测试时遇到此错误,我相信

I am getting this error when I run one of my tests, and I believe it's coming from how I re-named my Solution and Projects recently.

我将它们全部从 DCIM_With_Test重命名为 DCIM,(更改了目录,文件名等。),但它仍然在各处引起不一致。

I renamed them all from 'DCIM_With_Test' to 'DCIM', (Changed the directories, file names etc.) but it is still causing inconsistencies here and there.

另一个原因可能是导致问题的方法运行的方式。我已将方法从文本文件读取更改为从 app.config 文件读取,并且我对该过程和XML还是陌生的,所以我可能在某个地方缺少步骤。

Another cause could possibly be the way the method that is causing the issue is running. I have changed the method from reading from a text file to reading from an app.config file and I am new to this process and XML so I could be missing a step somewhere.

这是我的app.config:

Here is my app.config:

<?xml version="1.0"?>
<configuration>
<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
  <connectionStrings>
    <add name="mySqlConnectionString" connectionString="server=localhost;database=dcim;uid=root;pwd=LlmD62jL;"/>
  </connectionStrings>
</configuration>

我也收到有关app.config的三则消息:

I am also getting three messages about app.config:



  1. 找不到元素'supportedRuntime'的架构信息。

  2. 找不到有关元素的架构信息属性'version'。

  3. 找不到属性'sku'的架构信息。


这里是正在测试的方法注意:旧方法运行良好,但测试仍会引发异常,但新方法根本无效。

Here is the method being tested NOTE: The OLD METHOD runs fine, but the test still throws the exception, but the NEW METHOD doesn't work at all.

public static string GetConnectionString()
{
    /* OLD METHOD
    StreamReader rdr = new StreamReader(@"C:\Users\Benjamin\Documents\setupfile.txt");
    mySqlConnectionString = rdr.ReadToEnd();
    rdr.Close();
    */

    // NEW METHOD    
    mySqlConnectionString = ConfigurationManager.ConnectionStrings["mySqlConnectionString"].ConnectionString;

    return mySqlConnectionString;
}

这是测试:

[TestMethod()]
public void ConnectionStringTest()
{
    int actual = 0;
    int expected = 1;
    Database_MySQLDef.GetConnectionString();
    if (Database_MySQLDef.mySqlConnectionString != "")
    {
        actual = 1;
    }
    else { actual = 0; }

    Assert.AreEqual(expected, actual);
}


推荐答案


您的测试程序集是否具有app.config?

Does your test assembly have an app.config?

我的测试项目没有,但我的主项目却有。不确定这是同一回事吗?

my test project doesn't but my main project does. Not sure if that's the same thing?

不,不是。我怀疑正在发生的是 ConfigurationManager.ConnectionStrings 转到测试程序集的应用程序配置(我相信这是MSTest的行为),而不是主项目的应用程序配置。然后,由于不存在该异常,您将在静态初始化程序或静态构造函数中引发异常,并由TypeLoadException包装该异常。

No, they aren't. I suspect what is happening is that ConfigurationManager.ConnectionStrings is going to the test assembly's app config (this is MSTest's behavior, I believe), not the your main project's app config. Then, because that doesn't exist, you are getting an Exception thrown in a static initializer or static constructor and this is wrapped by the TypeLoadException.

一种简单的解决方法是将一个app.config添加到您的测试程序集。然后,您可以输入测试所需的任何连接字符串。

A simple way to solve this is to just add an app.config to your test assembly. Then you can put in whatever connection string you need for the tests.

这篇关于System.TypeLoadException:无法从程序集“ x”中加载类型“ x”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆