Compile a C# project into one dll
I have a base project that feeds two different projects. I want to be able to have the primary project c开发者_如何学Pythonompile 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.
(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.
Have a look for the external tool ILMerge from Microsoft: http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx It can merge multiple assemblies into just one.
You can use ILMerge for merging multiple assemblies:
ILMerge is a utility for merging multiple .NET assemblies into a single .NET assembly. It works on executables and DLLs alike and comes with several options for controlling the processing and format of the output. See the accompanying documentation for details.
You could merge the base assemblies together and use the resultant assembly as a reference to your other projects.
Alternatively, instead of two projects, have your shared code placed in one project in order to get a single assembly to reference.
精彩评论