ReflectionTypeLoadException : Could not load file or assembly ‘[DLL Name]’ or one of its dependencies

Getting a Reflection Type Load Exception, and confused by the message or simply can’t figure out what the issue is? As Microsoft defines the exception as, “The exception that is thrown by the Module.GetTypes method if any of the classes in a module cannot be loaded…” which is pretty broad. After debugging a few different types of this exception, I’d thought I’d lend a hand on what I’ve learned.

What to Look For

When checking for load errors, you can catch the ReflectionTypeLoadException. For example, the following is one way to catch the exception, and print out each one of there are multiple.

try
{
        //Some code loading up the application
}
catch (ReflectionTypeLoadException ex)
{
	foreach (var item in ex.LoaderExceptions)
	{
                //Could do something different here, just outputting
                // in a messagebox for debugging purposes
		MessageBox.Show(item.Message.ToString());
	}
}

If you are debugging on an older machine and the ReflectionTypeLoadException is not getting thrown, you’ll have to catch it in a general exception. In the following example, my inner exception was throwing a Load Exception, so I converted to list the loader exceptions (When trying to cast the exception, it was getting null, so I had to move to the inner exception) :

try
{
	//Some code loading up the application
}
catch (Exception e)
{
	if (e.InnerException != null)
	{                    
		ReflectionTypeLoadException LoadException = e.InnerException as ReflectionTypeLoadException;
		foreach (Exception ex in LoadException.LoaderExceptions)
		{
			MessageBox.Show(ex.Message, "LoadException.LoaderExceptions");
		}		
	}
}

There are ways to actually check if it’s a Loader Exception, this is more of a simpler example for the quick and dirty method. Now let’s review some of the common messages and what you can do .

Could not load file or assembly ‘[DLL Name]’ or one of its dependencies. The system cannot find the file specified.

This one is pretty obvious, but I thought I’d go over it anyway. When you get this message, it means that you are referencing a DLL in your project that it can not find while attempting to load your application. This can be due to a missing or corrupted file. If you are getting this on a client machine, make sure to one of the following :

  •  Zip or compress the files in someway so when you transfer, so you are sending a single object with no chance of error on a specific file.
  • Make sure any third-party tools that are in your GAC are in your bin folder that you are deploying manually on a client’s machine. This will usually be taken care of if you build an install project, but not always the case.

Could not load file or assembly ‘[DLL Name]’ or one of its dependencies. The module was expected to contain an assembly manifest

This message is probably the most common, also the most cryptic. This message will usually appear if the application can find your file, but is corrupted or an issue with the project. There are more solutions then listed here, but you can try one of the following :

  • Rebuild your code, something didn’t build right. Something to note here, don’t just build the solution, rebuild it. This will perform a clean and a build.
  • Reinstall the application on the client machine
  • Look in your solution to see if there is a type/build mismatch. (Right click solution -> Properties -> Configuration Properties -> Configuration)
  • Are you targeting 64 bit, but trying install on a 32 bit machine? If you are targeting “Any CPU” try targeting x86 instead and test.
  • Ensure you have the highest .Net Framework installed.

Could not load file or assembly ‘[DLL Name]’ or one of its dependencies.An attempt was made to load a program with an incorrect format.

Similar to the above message, this will occur most likely when you have a type mismatch or a vague dependency issue. Best course of action :

  • Rebuild the application. Sometimes Visual Studio will encounter an error with the dependencies, on all that is needed is a fresh build of the application
  • Look in your solution to see if there is a type/build mismatch. (Right click solution -> Properties -> Configuration Properties -> Configuration)
If this did not fix your error, try enabling the debugging versus catching the error. Debug -> Exceptions… -> Check Common Language Runtime Exceptions

Well, that’s all I’m going to cover for now. Hopefully this helps and happy coding!

Jacob Saylor

Software developer in Kentucky

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: