Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Exception Thrown at (not in) Constructor

Tags:

c#

exception

An exception is being thrown at (not in) a constructor call:

controller = new Controller(classInstance);   // Won't let me step in, exception thrown immediately.

The exception description is:

System.BadImageFormatException: Could not load file or assembly 'ClassLib, Version=1.0.4314.17265, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

classInstace isn't null and I cannot step into the constructor. I have never experienced this before. Any idea what this could be?

Thanks!

like image 538
john Avatar asked Dec 06 '25 08:12

john


2 Answers

Try using the Fusion Log Viewer (fuslogvw.exe) to see which assembly's being matched to ClassLib. Like Jon Skeet said, it's probably either a corrupted file, a 32 vs/ 64-bit issue, or a weird runtime mismatch.

like image 169
MNGwinn Avatar answered Dec 08 '25 22:12

MNGwinn


Yes - it can't find the ClassLib assembly which presumably contains Controller - or one of its dependencies. The BadImageFormatException suggests that maybe you've got a broken file (copy failed?) or possibly you're trying to load a .NET 4 assembly into a .NET 2 CLR. (I don't know whether that gives that exception or not, but I wouldn't be surprised.)

You haven't said what kind of project this is, but basically check all of your dependencies.

like image 31
Jon Skeet Avatar answered Dec 08 '25 20:12

Jon Skeet