什么可能导致 Visual Studio 单元测试中的 System.TypeLoadException? [英] What could be causing a System.TypeLoadException in a Visual Studio Unit Test?

查看:31
本文介绍了什么可能导致 Visual Studio 单元测试中的 System.TypeLoadException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编译良好的 C# .NET 类库 MyClassLibrary.我正在尝试为其创建一个单元测试项目(使用 Visual Studio 单元测试框架和 Visual Studio 2010).类库中确实有大类,但是每当我针对最简单的类运行最简单的测试时,都会出现以下异常:

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:

测试方法 MyClassLibraryTest.MyClassLibraryTests.MySimpleClassTest 抛出异常:System.TypeLoadException:无法从程序集MyClassLibrary,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"加载类型MyClassLibrary.MySimpleClass".

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'.

我正在处理的所有项目都在同一个解决方案中,并且都是为 .NET 4.0 编译的.所有这些都在 Windows 7 64 位机器上.

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?

推荐答案

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.

这篇关于什么可能导致 Visual Studio 单元测试中的 System.TypeLoadException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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