Compile a C# project into one dll

Omnia9 picture Omnia9 · Aug 4, 2011 · Viewed 13.5k times · Source

I have a base project that feeds two different projects. I want to be able to have the primary project compile all the references into one dll at compile time.

This needs to be something that is built into the project build so that Jr developers don't have to worry about it.


Solution: I ended up adding a line to the Post-build action of the project, so that a good build does the following:

$(ProjectDir)ilmerge.exe /out: /ver: <.dll> <.dll>

ilmerge.exe was moved to the project and the above line takes the dlls given and creates one.

Answer

Abel picture Abel · Aug 4, 2011

(this answer was edited to update links to new download locations and to reflect new insights)

Your best bet is to use Micorosoft's ILMerge. It used to be available as a separate download from Microsoft Research (it is not part of VS or the .NET SDK).

The original version was maintained until 2012. It is now a NuGet package, which is the best way to get it presently, and to add it to your project and build server.

As an alternative, you may want to try ILMerge GUI from CodePlex, which provides a GUI interface to ILMerge. Its development was interrupted, but is now active again.

A Codeproject article on ILMerge explains more on how to use it and explains some use-case. An example command is:

ilmerge /target:winexe /out:AllInOne.exe NormalProgram.exe Lib1.dll Lib2.dll

As an alternative, you can use Fody with Costura.Fody, as explained in this SO response.


Note (1): if you need to merge side-by-side assemblies (i.e., mix native 64 bit and 32 bit assemblies), you should consult this SO article on loading side-by-side assemblies dynamically.

Note (2): to add this as an integral step to your build process, just go to Project Properties > Build Events > click "Edit Post-build" and fill in the command from above. Click "Macros" to see how to reference the current file or assembly.