什么可能导致 Visual Studio 单元测试中出现 System.TypeLoadException?
我有一个可以正常编译的 C# .NET 类库 MyClassLibrary 。我正在尝试为其创建一个单元测试项目(使用 Visual Studio 单元测试框架和 Visual Studio 2010)。类库中确实有大类,但是每当我对最简单的类运行最简单的测试时,我都会得到以下异常:
测试方法 MyClassLibraryTest.MyClassLibraryTests.MySimpleClassTest 抛出异常: System.TypeLoadException:无法从程序集“MyClassLibrary,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”加载类型“MyClassLibrary.MySimpleClass”。
我正在处理的所有项目都在同一个解决方案中,并且都是针对 .NET 4.0 进行编译的。所有这些都在 Windows 7 64 位计算机上进行。
这是奇怪的部分:当我“运行”测试时,我收到上述错误。但是当我“调试”测试时,它运行良好。为什么?
I've got a C# .NET class library MyClassLibrary that compiles fine. I'm trying to create a unit test project for it (using Visual Studio Unit Testing Framework, with Visual Studio 2010). The class library does have big classes in it, but whenever I run even the simplest test against the simplest class, I get the following exception:
Test method MyClassLibraryTest.MyClassLibraryTests.MySimpleClassTest threw exception:
System.TypeLoadException: Could not load type 'MyClassLibrary.MySimpleClass' from assembly 'MyClassLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
All of the projects I'm dealing with are in the same solution, and all are compiled for .NET 4.0. All of this is on a Windows 7 64-bit machine.
Here's the weird part: when I "Run" the test, I get the above error. But when I "Debug" the test, it runs fine. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我刚刚用头撞了这个东西一个小时。问题是我有一个名为 Something.exe 的命令行项目,它使用名为 Something.dll 的类库项目。
I just banged my head against this one for an hour. The problem was that I had a command line project named Something.exe, which was using a class library project named Something.dll.
我也发生过。就我而言,出现问题是因为测试项目和单元测试项目具有相同的名称。
如果您也是这种情况,请重命名其中一个项目并重命名输出文件名来修复它。
Happened to me too. In my case the problem arised because the tested project and the unit tests project had the same name.
If this is your case too then rename one of the projects and rename output file name to fix it.
MyClassLibrary 程序集在配置管理器中设置为 x86 模式。将其更改为 x64 修复了它。我真的希望 Visual Studio 能够检测到这一点并将其报告为一个不太晦涩的错误。
The MyClassLibrary assembly was set to x86 mode in the configuration manager. Changing this to x64 fixed it. I really wish Visual Studio would detect this and report it as a less obscure error.
我也发生过。它与 x64、Release 和 x86 模式的构建相关。就我而言,我删除了 bin 中的文件夹(参考程序集和单元测试中的 debug/release/x86)并重新运行我的单元测试。 VS2010在输出窗口中有些报告错误。这为我解决了这个问题。
Happened to me too. It is related to building for x64, Release and x86 mode. In my case, I deleted folders in my bin (debug/release/x86 in reference assemblies and unit test) and re-run my unit test. VS2010 somewhat reported the error in Output Window. That solved it for me.
当我尝试添加具有相同主项目名称的测试项目时,发生了这种情况。
我的主要项目名称是:Calculator.OperationsManager
我的测试项目名称是:Calculator.OperationsManager
我已将测试项目名称更改为 Calculator.OperationsManager.Test,一切顺利。
It happened to me when i was trying to add test project with the same main project name.
My main project name was : Calculator.OperationsManager
my test project name was : Calculator.OperationsManager
i have changed the test project name as Calculator.OperationsManager.Test and everything went well.
今天经历了这个,但我会留下我的修复。
规格:VS 2013 / .Net 4.0
解决方案:进入菜单 >测试>测试设置>默认处理器架构> X64
Came through this today and though I would leave my fix.
Specs: VS 2013 / .Net 4.0
Solution: Go into Menu > Test > Test Settings > Default Processor Architechture > X64
这里有同样的消息,但我也无法调试测试。
就我而言,我正在测试的 DLL 已部署到 GAC(BizTalk 要求)。我创建了一个新类并正在测试它,但自从添加了待测试的类后,GAC 就没有再次对 DLL 进行操作。
Same message here, but I couldn't Debug the test either.
In my case, the DLL I was testing was deployed to the GAC (a BizTalk requirement). I had created a new class and was testing it, but hadn't GAC'd the DLL again since adding the class under test.
我遇到了与 Trey 类似的问题,但我没有使用 BizTalk,而是使用了也使用 GAC 部署的 SharePoint 解决方案。 GAC 的组件较旧。当我通过收回解决方案来移除 GAC 组件时,测试通过了。
I had similar problem as Trey, but instead of BizTalk, I have SharePoint solution which also uses GAC deployment. GAC had an older assembly. When I removed the GAC assembly by retracting the solution, the test passed.
如果这可以帮助其他人解决相同的错误(我意识到它并没有直接回答发布与调试的问题);我合并了一个遗留项目,其名称空间与现有名称空间冲突,因此将其重命名。尝试从该项目创建表单时出现此错误。
我检查了平台目标是否相同,并删除了 .\bin\ 目录以确保干净重建,删除了对合并项目的引用并重新添加它,但仍然是相同的错误。
最终(!)我检查了项目属性中的程序集名称(右键单击项目,选择“属性”,选择“应用程序”选项卡)并将其更改为匹配默认命名空间,现在一切正常。
In case this helps others with the same error (I realise it doesn't directly answer the question re Release vs Debug); I merged in a legacy project with a namespace that conflicted with an existing one, so renamed it. I got this error when trying to create a form from that project.
I checked the platform target was the same, and deleted the .\bin\ directories to ensure a clean rebuild, removed the reference to the merged project and re-added it, but still the same error.
Eventually (!) I checked the Assembly Name in the project's properties (right click on project, select 'properties', select the 'Application' tab) and changed that to match the Default namespace, and all is now well.
我在 VS 2013 中使用 NUnit 3 时遇到此错误。我通过删除测试项目中包含未找到的类型的程序集的程序集引用,然后重新添加引用来解决该错误。
I had this error using NUnit 3 in VS 2013. I solved it by deleting the assembly reference in my test project for the assembly that contained the type that was not being found, and then re-added the reference.
以防万一有人需要这个:
我在 Visual Studio 中创建了一个测试项目,并想知道为什么即使 Visual Studio 反复要求我添加“System.Web”作为引用,也找不到某些类。
我已经这样做了,但错误仍然发生。对我来说问题很简单(我知道我之前应该检查过这一点):我已经从创建 .NET Core 项目的模板创建了一个测试项目。将其更改为 .NET Framework 4.6.1 并添加“System.Web”作为参考后,一切正常。
Just in case anyone needs this:
I had created a test project in Visual Studio and had wondered why some Classes couldn't be found even if Visual Studio repeatedly asked me to add "System.Web" as reference.
I had done that and the error kept occurring. The problem for me was simple (and I should have checked that before, I know): I had created a test project from a template that created a .NET Core project. After changing it to .NET Framework 4.6.1 and adding "System.Web" as reference, everything worked fine.
遇到了同样的问题。以防万一它对任何人有帮助 - 我设法通过将 nuget 包 NUnit3TestAdapter 从版本 3.13.0 降级到 3.11.2 来使其工作。
您可以找到更多相关信息 - https://github.com/nunit/nunit-控制台/问题/424
Encountered the same issue. Just in case if it helps anyone - I managed to get it to work by downgrading the nuget package NUnit3TestAdapter from version 3.13.0 to 3.11.2.
You can find more information on this - https://github.com/nunit/nunit-console/issues/424
这也发生在我身上。我在我的域项目上安装了版本 1.5 的 nuget 包,但在我的单元测试项目上只安装了版本 1.1。我通过更新单元测试项目上的版本以匹配域项目(正在测试的项目)上的版本解决了该问题。
This happened to me as well. I had a nuget package installed on my domain project with version 1.5, but only had version 1.1 on my unit test project. I resolved the issue by updating the version on the unit test project to match the version on the domain project (the project being tested against).
就我而言,混淆的代码导致了这个问题。我已禁用代码混淆并解决了问题。
In my case obfuscated code caused the issue. I have disabled the code obfuscation and resolved the problem.