How to debug System.TypeLoadException errors in .NET?

十年热恋 提交于 2019-11-26 16:39:41

问题


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

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

I'll be honest, I don't think I can remember the last time I saw a System.TypeLoadException error, or if I saw it, the solution was obvious. 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.

Perhaps I'm reading this wrong, but it 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 ontop of .NET4 with VS2010, so there's no mono or other weird library issues. Am I over thinking this? What's going on?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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



回答6:


I encountered this exception when I refactored to move some classes to their own library, and referenced a different version of the library it's trying to load in the new library (acquired with NuGet) than I had been using in the original project.

The thing that fixed it was to open the NuGet Manager in the old project, and update the appropriate package to be the same version I was using in my new library.




回答7:


This problem may occur if you have different versions of NuGet packages installed across different projects. To identify these cases,

  1. Right-click the solution in Solution Explorer and select "Manage Nuget Packages for Solution"
  2. Select the "Consolidate" tab
  3. For any identified packages, identify the version you want and click "Install" to consolidate the package to that version.


来源:https://stackoverflow.com/questions/5691100/how-to-debug-system-typeloadexception-errors-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!