Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assembly.LoadFrom exception for exe

I’m porting a .Net Framework project to .Net 8 and encountered an issue with Assembly.LoadFrom(). The loaded assembly is used to list dependencies and its versions.

.Net always creates an exe file and a dll file (e.g. MyProgram.exe and MyProgram.dll)

Loading the exe fails with ‘Bad IL format. The format of the file ‘…’ is invalid.’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var myAssembly = Assembly.LoadFrom(@"C:\Test\MyProgram.exe");

Loading its corresponding dll works fine

var myAssembly = Assembly.LoadFrom(@"C:\Test\MyProgram.dll");

Assuming that the exe is just a loader for the dll it can’t have a different IL format.
Please correct me if I’m wrong with that. Might be a lack of understanding…

>Solution :

Assuming that the exe is just a loader for the dll it can’t have a different IL format.

exe file is a native file. Normally it doesn’t have IL format at all, just native code and some additional data.

Native formats are very flexible and it is possible to put arbitrary data into them, next to native code. Including dlls. And the old .Net Framework did exactly that. But the newer .Net does not do this by default. It creates a native small loader for the corresponding dll file. So it separates loader from compiled bytecode.

You can enable this merging of exe with dll through single-file deployment but I’m not sure if Assembly.LoadFrom will work with it. I didn’t test it.

Anyway, if you have scenario with dll next to exe, then just load the dll.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading