I had a need to create a simple .NET app, but it needed to be self contained in a single EXE. Native developers (C / C++) can easily create single EXE programs by utilizing the linker to pull the various pieces into the EXE. Not so easy for .NET.
I remembered reading about options for this in the past, and decided to take the quick & easy route. Options that I’ve heard of include:
- Use the Mono tooling to “link” the dlls into the exe
- Hook “type-resolution” events and locate the types in DLL embedded resources (no need to put dlls on disk)
- Use embedded DLLs and simply copy them to the current directory on startup
Quick and easy for me is the last option. I embedded the DLL in the EXE and then extracted it to the file system on app startup.
NET’s type resolution seems to work on a class by class basis. .NET wants to resolve all dependencies of the class, even if you have not yet instantiated the referenced type. So… Move the logic that uses the referenced type into a separate class. Make sure to extract dlls from the EXE before creating an instance of that class and you should be good to go.