What could be causing a System.TypeLoadException?

79,068

Solution 1

It could be any number of things. Likely causes are:

  • The assembly cannot be found
  • An assembly that your assembly depends upon cannot be found
  • The assembly is found but the type isn't in it
  • The type's static constructor throws an exception

Your best bet is to use the Fusion log viewer to help diagnose it. Documentation is here:

http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx

(FYI "Fusion" was the code name of the team that designed the assembly loading system; it is somewhat unfortunate that the code name ended up in the file name of the shipped product. The thing should have been called "AssemblyBindingLogViewer.exe" or some such thing.)

Solution 2

The answer of Eric Lippert perfectly describes the situation.

I just want to add a quick answer about a case which is usually not covered by help pages regarding this exception.

I've created a quick & dirty test project for some open source stuff (Akka.Net, to name it) and I name the project itself "Akka".

It perfectly builds, but at startup it throws it type load exception regarding a class in Akka.dll.

This is just because my executable (akka.exe) and the reference (akka.dll) have the same name. It took me a few minutes to figure this (I've began by things such as copy local, target platform, exact version... etc).

It's something very dumb but not forcibly the first thing which you will think (especially since I used nuget for dependancies), so I thought it could be interesting to share it : you will encounter TypeLoadException if your EXE and a dependancy have the same name.

Solution 3

This almost drove me crazy...

I don't know how I managed this, but for some reason I had an old version of the DLL in GAC. Try looking for an old assembly there and remove it.

Best of luck!

Solution 4

This could be caused by any number of things, MSDN has it said as:

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

So it's clear that a type can't be found, either the assembly is missing, the type is missing or there's a clash between runtime configurations.

Sometimes the issue can arise because the assembly you're referencing is a different platform type (32bit / 64bit etc) than the one you're consuming from.

I would recommend catching the exception and examining it in more detail to identify what it's having trouble with.


Further to my previous information

Sometimes I've seen this issue arise because (for one reason or another) a referenced assembly can't actually be resolved, even though it's referenced and loaded.

I typically see this when AppDomain boundaries are involved.

One way I've found that sometimes resolves the issue (but only if the assembly is already in the AppDomain) is this code snippet:

AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
{
   return AppDomain.CurrentDomain.GetAssemblies()
      .SingleOrDefault(asm => asm.FullName == e.Name);
}

Like I said, I see this issue when AppDomains get involved and this does seem to solve it when the assembly is indeed already referenced and loaded. I've no idea why the framework fails to resolve the reference itself.

Solution 5

You can get loader log files out of the .NET Compact Framework by enabling some settings in the registry. The Power Toys for .NET Compact Framework include a tool called NETCFLogging, which sets the registry values for you.

The registry values are documented here:

http://msdn.microsoft.com/en-us/library/ms229650(v=VS.90).aspx

Share:
79,068
Mohamed Jihed Jaouadi
Author by

Mohamed Jihed Jaouadi

Updated on October 01, 2021

Comments

  • Mohamed Jihed Jaouadi
    Mohamed Jihed Jaouadi over 2 years

    I'm developing, with VS2008 using C#, an application for Honeywell Dolphin 6100, a mobile computer with a barcode scanner that uses Windows CE 5.0 like OS.

    I want to add a functionality that can send files from the local device to the distant server. I found the library "Tamir.SharpSSH" which can guarantee this. I tested the code on a console application and on normal windows forms application and it works perfectly. But when I tried to use the same code on the winCE device, I get a TypeLoadException and I have the error message:

    Could not load type 'Tamir.SharpSsh.SshTransferProtocolBase' from assembly 'Tamir.SharpSSH,   
    Version=1.1.1.13, Culture=neutral, PublicKeyToken=null'.
    

    the code that I'm use is like below :

    SshTransferProtocolBase sshCp = new Scp(Tools.GlobalVarMeth.hostName, Tools.GlobalVarMeth.serverUserName);
    sshCp.Password = Tools.GlobalVarMeth.serverUserpassword;
    sshCp.Connect();
    
    string localFile = Tools.GlobalVarMeth.applicationPath + "/" + fileName + ".csv";
    string remoteFile = Tools.GlobalVarMeth.serverRemoteFilePath + "/" + fileName + ".csv";
    
    sshCp.Put(localFile, remoteFile);
       
    sshCp.Close();
    

    Any one have any idea on this ? I will be really grateful !!!

  • Thomas
    Thomas over 8 years
    For me, it was the static constructor throwing an exception. Thank you.
  • HGMamaci
    HGMamaci about 7 years
    In my case the DLLs at target machine were older. After updating them it worked fine.
  • smoothumut
    smoothumut almost 7 years
    my problem was type's static constructor that throws an exception. I have ignored it until I saw your answer. Thanks a lot
  • miki
    miki almost 7 years
    This was problem in my case - main .exe project references another project in the same solution, but Assembly name was the same. Thanks!
  • Zapnologica
    Zapnologica over 6 years
    what is GAC stand for?
  • Hudson
    Hudson over 6 years
    Global Assembly Cache
  • Mike
    Mike over 5 years
    This naming caused the same exception for me. I found this answer and see that I had already upvoted it from when naming did this to me a year ago!
  • Philipp Sch
    Philipp Sch almost 5 years
    This was also the problem in my case. So the post fixed it for me.