Click here to Skip to main content
Click here to Skip to main content

Tagged as

Combining Multiple .NET Assemblies

, 20 Mar 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
Combining multiple .NET assemblies by customizing MSBuild's project file.

Introduction

Whoever searched for a solution to merge multiple assemblies into a single file probably heard of tools like ILMerge, SmartAssembly, etc.
Another known solution is embedding the DLLs as resources (if anyone's interested, here is a nice article that explains this approach: Load DLL From Embedded Resource[^]).

However on few occasions, I noticed an unnecessary use of these approaches.
If we have these assemblies source codes, then we can achieve the combining by importing all the source code files into a single project at compile time.

In this tip, I'll try to explain briefly how to accomplish that.

For demonstration purposes, let's say we have a console application (our main assembly) that references and uses two class libraries (our secondary assemblies) and we want to combine them all into a single file:

When building this solution, we get three assemblies as expected:

Note that MyExecutable's project file (MyExecutable.csproj) is an XML based file and if we inspect its contents, we can find few ItemGroup nodes. These nodes contain child elements that define the build processes inputs. These child elements can refer to application's source files that need to be compiled, or resource files that need to be copied or assemblies that need to be included in the build process (if anyone's interested, you can read more about Visual Studio project files on MSDN MSBuild).

Now let's locate the ItemGroup node that refers to our included assemblies:

<ItemGroup>
  <ProjectReference Include="..\MyLibrary1\MyLibrary1.csproj">
    <Project>{ea53ca82-13d7-4be1-b95a-4d9d7853d46e}</Project>
    <Name>MyLibrary1</Name>
  </ProjectReference>
  <ProjectReference Include="..\MyLibrary2\MyLibrary2.csproj">
    <Project>{c31d21f3-e86a-4581-b4e8-acae6644d19e}</Project>
    <Name>MyLibrary2</Name>
  </ProjectReference>
</ItemGroup>

Here, we will add a condition that will indicate to MSBuild to use these project references when building MyExecutable in Debug mode:

<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">

But for Release mode, we will include all the source code files from both MyLibrary1 and MyLibrary2 to be compiled as well. We will do this by using a wild card ("\**\*.cs") that will include all the CS files in the directory and its subdirectories. The wild card will also include some unwanted source code files (for this case, those are TemporaryGeneratedFile_[guid].cs files in obj folder and AssemblyInfo.cs file in Property folder) so we will have to exclude them:

<ItemGroup Condition=" '$(Configuration)' == 'Release' ">
  <Compile Include="..\MyLibrary1\**\*.cs"
           Exclude="..\MyLibrary1\Properties\AssemblyInfo.cs;
                    ..\MyLibrary1\obj\**;
                    ..\MyLibrary1\bin\**">
    <Link>MyLibrary1\%(RecursiveDir)%(Filename)%(Extension)</Link>
    <Visible>false</Visible>
  </Compile>
  <Compile Include="..\MyLibrary2\**\*.cs"
           Exclude="..\MyLibrary2\Properties\AssemblyInfo.cs;
                    ..\MyLibrary2\obj\**;
                    ..\MyLibrary2\bin\**">
    <Link>MyLibrary2\%(RecursiveDir)%(Filename)%(Extension)</Link>
    <Visible>false</Visible>
  </Compile>
</ItemGroup>

And that is it, let's save these changes in MyExecutable.csproj file and rebuild the solution in Release mode:

Last thing I would like to emphasize as a sort of troubleshooting advice, because we are literally moving the compilation of all assemblies source files into a single project, that project needs to be able to compile those files. So you need to consider the following:

  • The main assembly needs to have all the references, resources, settings, etc. of the secondary assemblies in order to build successfully.
  • All assemblies need to be written in the same .NET language.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mario Z
Software Developer GemBox Software
Croatia Croatia
I'm a developer at GemBox Software, working on:
  • GemBox.Document - .NET Word component for reading, writing, converting and printing DOC, DOCX, PDF, HTML, XPS, RTF and TXT files.
  • GemBox.Spreadsheet - .NET Excel component for reading, writing, converting and printing XLS, XLSX, PDF, HTML, XPS, ODS and CSV files.
Follow on   Google+   LinkedIn

Comments and Discussions

 
QuestionWhat's the use? PinmvpSergey Alexandrovich Kryukov25-Mar-15 5:26 
AnswerRe: What's the use? PinprofessionalMario Z25-Mar-15 8:27 
GeneralRe: What's the use? PinmvpSergey Alexandrovich Kryukov25-Mar-15 8:53 
GeneralRe: What's the use? PinprofessionalMario Z25-Mar-15 12:45 
GeneralRe: What's the use? PinmvpSergey Alexandrovich Kryukov25-Mar-15 13:26 
GeneralRe: What's the use? PinprofessionalMario Z25-Mar-15 16:30 
GeneralRe: What's the use? PinmvpSergey Alexandrovich Kryukov25-Mar-15 17:40 
GeneralRe: What's the use? PinprofessionalMario Z26-Mar-15 8:24 
GeneralRe: What's the use? PinmvpSergey Alexandrovich Kryukov26-Mar-15 10:05 
GeneralRe: What's the use? PinprofessionalMario Z27-Mar-15 6:05 
GeneralNow it's clear, but... PinmvpSergey Alexandrovich Kryukov27-Mar-15 13:28 
GeneralRe: Now it's clear, but... PinprofessionalMario Z28-Mar-15 0:42 
GeneralAgain: it won't build PinmvpSergey Alexandrovich Kryukov28-Mar-15 1:07 
GeneralRe: Again: it won't build PinprofessionalMario Z28-Mar-15 1:17 
GeneralRe: Again: it won't build PinprofessionalMario Z31-Mar-15 4:08 
GeneralRe: Again: it won't build PinmvpSergey Alexandrovich Kryukov31-Mar-15 4:44 
GeneralRe: Again: it won't build PinprofessionalMario Z31-Mar-15 4:55 
GeneralRe: Again: it won't build PinmvpSergey Alexandrovich Kryukov31-Mar-15 4:59 
GeneralIt works, failed to reproduce the problem PinmvpSergey Alexandrovich Kryukov31-Mar-15 5:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.1 | Last Updated 20 Mar 2015
Article Copyright 2015 by Mario Z
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid