Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug System.TypeLoadException errors in .NET?

Tags:

c#

clr

I'm getting the following error on one of my referenced assemblies:

Could not load type 'System.Func`2' from assembly 'MyAssembly, ...

My first instinct was to see what MSDN had to say about it:

TypeLoadException is thrown when the common language runtime cannot find the assembly, the type within the assembly, or cannot load the type.

It appears to be is saying that the CLR simply can't find the type? That might make more sense if this wasn't something that was in mscorlib. This was all built on top of .NET4 with VS2010, so there's no mono or other weird library issues. What's going on?

like image 685
kertosis Avatar asked Apr 17 '11 02:04

kertosis


5 Answers

I received this error after refactoring. I had two projects compiling to DLLs with the same name.

Check the "Assembly name" in the project's properties' "Application" section.

like image 64
Ian Warburton Avatar answered Nov 16 '22 01:11

Ian Warburton


The problem is that you have a mismatch in your versions. Make sure all your assemblies are compiled for .NET 4.

like image 41
Gabe Avatar answered Nov 16 '22 00:11

Gabe


I'm not sure about your specific scenario, but the Assembly Binding Log Viewer (fuslogvw) is usually very helpful in debugging type load issues. More details at http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

like image 15
Ragesh Avatar answered Nov 16 '22 00:11

Ragesh


You may also get this if you change the assembly you're trying to load but still have an old version in the GAC. It tries to load the GAC'ed version not what you reference in your VS project.

like image 4
Jay Avatar answered Nov 16 '22 01:11

Jay


I got this error when I moved a class from one project to another in a cleanup effort. After looking at all other possible reasons, reloaded each of the projects in my solution and everything worked.

  1. Right Click on the project name in solution explorer
  2. Select Unload Project
  3. Right Click on the project name in solution explorer
  4. Select Reload Project
like image 2
RWL01 Avatar answered Nov 16 '22 01:11

RWL01