How can I troubleshoot : System.TypeLoadException?

18,284

Solution 1

In the exception message, or maybe in an InnerException property, you might find what was the requested type, with the fully qualified name, version, assembly. Check if the given assembly is reachable from the application directory. Reachable meaning in the application directory or in the private probing path of the application.

EDIT : also check that satellite assemblies (referenced by the assembly that the type loader fails loading) are reachable.

Solution 2

One situation where this exception is thrown is if the executing assembly has the same name as the assembly with the given type. For example, if you are running MyApp.exe and the type is in MyApp.dll, the CLR will assume that the type should be in MyApp.exe. A solution would be to rename one of those assemblies.

Solution 3

You could try switching on the 'Break when an exception is thrown' from Debug -> Tools -> Exceptions and click all the check boxes in the Thrown column. When you re-run your test you should be able to see exactly where this exception is being thrown.

Solution 4

Well... It was totally my bad.

I gave a wrong assembly name which was resulting a missing DLL in the bin folder.

Thanks to my mate, he pointed me to check the bin folder. And after checking the added reference's "path" I have noticed that it is looking to a completely different place.

Share:
18,284
vel
Author by

vel

Updated on June 05, 2022

Comments

  • vel
    vel almost 2 years

    Can you show me a way to troubleshoot System.TypeLoadException ?

    I am having this exception for an existing project in my solution which I reference from a unit test project in the same solution.

    This exeption is thrown when I run my unit tests. They fail because of this exception:

    Details:

    Test method MyErrorHandler.Test.MyTest.Parse_RecievesValidMessage_ReturnsArray threw exception: System.TypeLoadException: Could not load type 'MyTestNameSpace' from assembly 'MyTestAssemblyName.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=Somenumber.

    What should I check or where should I start to investigate?

  • Jon Comtois
    Jon Comtois about 10 years
    +1 Thank you this just saved my tail after a lot of head scratching!
  • David R Tribble
    David R Tribble almost 6 years
    Ditto. If nothing else, using the same name for the .dll and the .exe will cause the .pdb for the .dll to be overwritten. I simply add a 'Lib' suffix to the library, or better yet, add a 'Mgr' suffix to the .exe name.