Linking statically in C#

15,109

Solution 1

You can merge your many DLLs with ILMERGE:

http://research.microsoft.com/~mbarnett/ILMerge.aspx

Haven't tried it myself. Hope it helps.


Download here:
http://www.microsoft.com/downloads/details.aspx?familyid=22914587-B4AD-4EAE-87CF-B14AE6A939B0&displaylang=en

Brief Description (from download-page)
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.

Solution 2

If you don't want to use ILMerge, see this page:

http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

editor's note: Jeffrey Richter advices to put your dlls into exe file as resources (For each DLL file you add, display its properties and change its “Build Action” to “Embedded Resource.”). Then a custom class loader is needed to make the executable work (At runtime, the CLR won’t be able to find the dependent DLL assemblies, which is a problem. To fix this, when your application initializes, register a callback method with the AppDomain’s ResolveAssembly event).

Be sure to change the resourceName string to point to your actual resources. (e.g. change AssemblyLoadingAndReflection to your project name.)

Solution 3

The short answer for this is no! You can not link in a dll during compilation. I don't know if there is some subtle way to do this, but you would probably have to distribute the dlls along with your cms. The best way to do this is to make some kind of re-distributable.

Share:
15,109

Related videos on Youtube

erlando
Author by

erlando

Standards are important to me. I mostly use PHP, XHTML, CSS and Javascript to do my magic.

Updated on April 17, 2022

Comments

  • erlando
    erlando about 2 years

    I'm working on a module for a CMS. This module is distributed as a class library DLL.

    I have several utility libraries I'd like to use in this module. Is there anyway I can link these libraries statically so I won't have to distribute several DLL's (thereby distributing my utility libraries separately)?

    I would like to have only one DLL.

  • Brian Stewart
    Brian Stewart over 15 years
    As Seb mentioned ILMERGE will do this - but one caveat is that none of the assemblies can contain unsafe code.
  • Kobor42
    Kobor42 over 10 years
    So much better! Nativ solution over third party tool. Look at the comment section for the comment of the ILMerge creator...
  • Kobor42
    Kobor42 over 10 years
    Not true. Look at Kagamar's answer. Also look at the comment section for the comment of Mike Barnett.
  • CubicleSoft
    CubicleSoft about 10 years
    While this takes a little extra effort, this seems like the better solution over the accepted answer.
  • Stefan Steiger
    Stefan Steiger over 9 years
    @Kobor42: Wrong, this is not linking this is merging. While the most upvoted answer answers the intent of the question (and is thereby the most useful answer), khebbie's answer is actually the right one, as long as one would precisely answer the question asked. You cannot even link a dll in C/C++, you need a static archive to do that...
  • Warren  P
    Warren P over 8 years
    It's hilarious that this answer, which is technically correct has no upvotes. C# is a managed language, and even ILMerge is an assembly-merger not a linker. A linker, as understood by C++ and C developers, can not currently exist in .NET as it currently exists. However a ".Net native" technology as yet not released in mid 2015, may some day be released that allows a fully compiled (no MSIL anymore) application, which could then be statically linked.
  • user1703401
    user1703401 over 6 years
    Unsafe code is just fine. It won't deal with mixed-mode assemblies. The kind that contains unmanaged code and was created by a C++/CLI project.