How to combine multiple. NET Assemblies _ Practical Tips

Source: Internet
Author: User

Anyone who has ever looked for a solution that merges multiple programs into a single file may have heard of such tools as Ilmerge, smartassembly, and so on.

Another well-known scenario is to embed the DLL as a resource (if interested, here's an excellent article that describes this scenario: load from embedded resources dll[^]).

And in some cases, I realized that it was necessary to use these methods. If we had the source code for these compilations, we could get a perfect mix of these things by importing all these source code files into a project at compile time.

Here I will try to make a brief description of how to do it.

To demonstrate, let's say we have a console application (this is our main program) that references and uses two class libraries (our Level two program), and we want to combine them into one file:

We built this solution as envisioned and got the three programs:

Note that the Myexecutable project file (myexecutable.csproj) is an xml-based file, and if we look at its contents, we find some itemgroup nodes. These node blogs contain child elements that define the input of the build process. These child elements can refer to the resource files of the application that need to be compiled, or to some resources that need to be replicated, and to the assemblies that need to be included in the build process (if you are interested, you can learn more about the Visual Studio project files on MSDN MSBuild) .

Now let's navigate to the ItemGroup node, which references the assembly to be included:

<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 indicates that you want to use these project references to MSBuild when building myexecutable in debug mode:

<itemgroupcondition= "' $ (Configuration) ' = = ' Debug '" >

For release mode, all source code files from MyLibrary1 and MyLibrary2 are included and compiled. We will use a wildcard rune ("\**\*.cs") to include the directory and its subdirectories in all CS files. Wildcard runes also include some resource code files that we don't want (Temporarygeneratedfile_[guid].cs in the obj folder and AssemblyInfo.cs files in the property folder) So we're going 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>

That's it, let's save the changes in Myexecutable.csproj and rebuild the solution in Release mode:

The last thing I want to emphasize is that because we are formally moving all of the assembly source files to a project, the project needs to be able to compile those files. So you need to consider the following things:

In order to build success, the main program needs to have two-level program all references, resources, settings, and so on.

All assemblies need to be written in the same. NET language.

The above is the full content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.