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.’
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.