Embedding DLLs in a compiled executable

前端 未结 16 2710
情深已故
情深已故 2020-11-21 07:07

Is it possible to embed a pre-existing DLL into a compiled C# executable (so that you only have one file to distribute)? If it is possible, how would one go about doing it?<

相关标签:
16条回答
  • 2020-11-21 07:50

    Yes, it is possible to merge .NET executables with libraries. There are multiple tools available to get the job done:

    • ILMerge is a utility that can be used to merge multiple .NET assemblies into a single assembly.
    • Mono mkbundle, packages an exe and all assemblies with libmono into a single binary package.
    • IL-Repack is a FLOSS alterantive to ILMerge, with some additional features.

    In addition this can be combined with the Mono Linker, which does remove unused code and therefor makes the resulting assembly smaller.

    Another possibility is to use .NETZ, which does not only allow compressing of an assembly, but also can pack the dlls straight into the exe. The difference to the above mentioned solutions is that .NETZ does not merge them, they stay separate assemblies but are packed into one package.

    .NETZ is a open source tool that compresses and packs the Microsoft .NET Framework executable (EXE, DLL) files in order to make them smaller.

    0 讨论(0)
  • 2020-11-21 07:51

    I highly recommend to use Costura.Fody - by far the best and easiest way to embed resources in your assembly. It's available as NuGet package.

    Install-Package Costura.Fody
    

    After adding it to the project, it will automatically embed all references that are copied to the output directory into your main assembly. You might want to clean the embedded files by adding a target to your project:

    Install-CleanReferencesTarget
    

    You'll also be able to specify whether to include the pdb's, exclude certain assemblies, or extracting the assemblies on the fly. As far as I know, also unmanaged assemblies are supported.

    Update

    Currently, some people are trying to add support for DNX.

    Update 2

    For the lastest Fody version, you will need to have MSBuild 16 (so Visual Studio 2019). Fody version 4.2.1 will do MSBuild 15. (reference: Fody is only supported on MSBuild 16 and above. Current version: 15)

    0 讨论(0)
  • If they're actually managed assemblies, you can use ILMerge. For native DLLs, you'll have a bit more work to do.

    See also: How can a C++ windows dll be merged into a C# application exe?

    0 讨论(0)
  • 2020-11-21 07:54

    Neither the ILMerge approach nor Lars Holm Jensen's handling the AssemblyResolve event will work for a plugin host. Say executable H loads assembly P dynamically and accesses it via interface IP defined in an separate assembly. To embed IP into H one shall need a little modification to Lars's code:

    Dictionary<string, Assembly> loaded = new Dictionary<string,Assembly>();
    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
    {   Assembly resAssembly;
        string dllName = args.Name.Contains(",") ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll","");
        dllName = dllName.Replace(".", "_");
        if ( !loaded.ContainsKey( dllName ) )
        {   if (dllName.EndsWith("_resources")) return null;
            System.Resources.ResourceManager rm = new System.Resources.ResourceManager(GetType().Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
            byte[] bytes = (byte[])rm.GetObject(dllName);
            resAssembly = System.Reflection.Assembly.Load(bytes);
            loaded.Add(dllName, resAssembly);
        }
        else
        {   resAssembly = loaded[dllName];  }
        return resAssembly;
    };  
    

    The trick to handle repeated attempts to resolve the same assembly and return the existing one instead of creating a new instance.

    EDIT: Lest it spoil .NET's serialization, make sure to return null for all assemblies not embedded in yours, thereby defaulting to the standard behaviour. You can get a list of these libraries by:

    static HashSet<string> IncludedAssemblies = new HashSet<string>();
    string[] resources = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
    for(int i = 0; i < resources.Length; i++)
    {   IncludedAssemblies.Add(resources[i]);  }
    

    and just return null if the passed assembly does not belong to IncludedAssemblies .

    0 讨论(0)
提交回复
热议问题