简体   繁体   English

System.TypeLoadException:无法从程序集“ x”加载类型“ x”吗?

[英]System.TypeLoadException: Could not load type 'x' from assembly 'x'?

I would just like to apologise by the overload of information, I just want to provide anything that could be related, I'm not sure what's happening 我只想对信息过多感到抱歉,我只想提供任何可能相关的信息,我不确定发生了什么

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'. 测试方法DCIM_Test_1.MySQLDefTest.ConnectionStringTest引发异常:System.TypeLoadException:无法从程序集“ DCIM”中加载类型“ DCIM_With_Test.Database.Database_MySQLDef”,版本= 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. 运行我的一个测试时出现此错误,我相信这是由于我最近重新命名“解决方案和项目”而来的。

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. 我将它们全部从“ DCIM_With_Test”重命名为“ DCIM”(更改了目录,文件名等),但这仍然在这里和那里引起不一致。

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文件读取的方法,并且我对这个过程和XML还是陌生的,所以我可能在某个地方缺少步骤。

Here is my app.config: 这是我的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>

I am also getting three messages about app.config: 我还收到有关app.config的三则消息:

  1. Could not find the schema information for the element 'supportedRuntime'. 找不到元素“ supportedRuntime”的架构信息。
  2. Could not find the schema information for the attribute 'version'. 找不到属性“版本”的架构信息。
  3. Could not find the schema information for the attribute 'sku'. 找不到属性“ 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. 这是被测试的方法注意:OLD METHOD运行良好,但是测试仍会引发异常,但是NEW METHOD根本不起作用。

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;
}

And here is the test: 这是测试:

[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);
}

Does your test assembly have an app.config? 您的测试程序集是否具有app.config?

my test project doesn't but my main project does. 我的测试项目没有,但我的主项目却有。 Not sure if that's the same thing? 不确定这是同一回事吗?

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

A simple way to solve this is to just add an app.config to your test assembly. 一种简单的解决方法是将app.config添加到测试程序集中。 Then you can put in whatever connection string you need for the tests. 然后,您可以输入测试所需的任何连接字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.TypeLoadException:无法从程序集“y”加载类型“x” - System.TypeLoadException: Could not load type 'x' from assembly 'y' System.TypeLoadException:无法从程序集mscorlib加载类型&#39;System.Reflection.RuntimeReflectionExtensions&#39; - System.TypeLoadException: Could not load type 'System.Reflection.RuntimeReflectionExtensions' from assembly mscorlib Iron Python System.TypeLoadException:无法从程序集“System.Core”加载类型“System.Runtime.CompilerServices.Closure” - Iron Python System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.Closure' from assembly 'System.Core' RtFieldInfo.FieldType 导致 System.TypeLoadException:无法从程序集“A...”加载类型“SubClass”,因为格式无效 - RtFieldInfo.FieldType causes System.TypeLoadException: Could not load type 'SubClass' from assembly 'A...' because the format is invalid System.TypeLoadException:无法加载类型&#39;System.Func`2&#39; - System.TypeLoadException: Could not load type 'System.Func`2' InnerException 引发 TypeLoadException TypeLoadException:无法从程序集“System.ServiceModel”加载类型“System.UriTemplate”, - InnerException throws TypeLoadException TypeLoadException: Could not load type 'System.UriTemplate' from assembly 'System.ServiceModel, TypeLoadException: 无法从程序集中加载类型 IHttpResponseStreamWriterFactory - TypeLoadException: Could not load type IHttpResponseStreamWriterFactory from assembly C# System.TypeLoadException:无法加载类型,因为它在偏移处包含 object 字段 - 来自 Z2D50972FECD376129545507F1062089Z 框架的代码 - C# System.TypeLoadException: Could not load type because it contains an object field at offset - code from .net framework on .net core C#:未处理的异常。 System.TypeLoadException: 无法加载类型“System.Drawing.Color” - C#: Unhandled exception. System.TypeLoadException: Could not load type 'System.Drawing.Color' 未处理System.TypeLoadException:无法加载类型,因为该方法没有实现(没有RVA) - System.TypeLoadException was unhandled: Could not load type because the method has no implementation (no RVA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM