Jay Sullivan Jay Sullivan - 21 days ago 5
C# Question

What could be causing a System.TypeLoadException in a Visual Studio Unit Test?

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?

Answer

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.