Merging assemblies with ILRepack

2017-12-18

Typically, in .NET, to merge assemblies you would use ILMerge. ILRepack is another project you can use and that has had updates recently (and a lot of activity on Github).

I’ve found it useful to merge the lot of Dlls and Exe that can compose a simple app into a single executable file (think containers but without Docker), which facilitates using and distributing it. The new .csproj file format is a great simplification and allows for tasks (as in the previous versions) in a smaller more understandable file.

To use ILRepack in a project, here are the steps you can perform to include the task during your build:

1 – add the ILRepack package to the project you wish to merge files in
2 – validate that the AssemblyName element is defined in the property groups in your .csproj file
3 – in the .csproj, add the following target element

3.1 – the NetStandardLocation element is only required if you are targeting it or including a package that targets it.
3.2 – the /lib:@(NetStandardLocation) argument in the Exec command is only required by 3.1
3.3 – adjust the /out argument in the command to whatever you require.

4 – If you want to include the output in a package, define the package elements in a propertyGroup and include:

And that’s pretty much it. If you run build, ILRepack will be executed and build the merged file, and MSBuild will also wrap it in a NuGet package if you include the packaging step.