Load Multiple DLL as Single Exe in .NET/.NET Core

Load Multiple DLL as Single Exe in NET and NET Core

In this article, we will see how to Load Multiple DLLs as a Single Exe in .NET and .NET Core.

The requirement was to load or embed dynamically multiple DLL resources.

(Both external, third-party vendor, and internal DLL) including system DLLs together as a bundle to simplify the usage.

Today in this article, we will cover the below aspects.

If interested to know on how to achieve the same for .NET Core 3.1 or .NET 5 with just simple a configuration change.

Today in this article, we will see how to achieve the same using the regular .NET framework and for .NET Core < 2.2.

The below-mentioned code works perfectly fine for both .NET and .NET Core.

Create a Console App using < .NET Core 2.2 or .NET framework

Load Multiple DLL as Single Exe in NET and NET Core

Let’s say we have below two DLLs we want to load or embed into a single DLL or exe.

  • ExternalApp1.dll
  • ExternalApp2.dll

We shall put these two DLLs into our project folder “ resources” as shown below,

Load Multiple DLL as Single Exe in NET and NET Core

Step 1:

Right-click on each DLL and update “ Copy to Output” as false.

Step 2:

Update “Build action” as an Embedded resource

Steps 1 and 2 are shown below,

Load Multiple DLL as Single Exe in NET and NET Core

Step 3:

Go to Project file -> Add Reference -> Browse->Select the DLLs from the below location,

Load Multiple DLL as Single Exe in NET and NET Core

Again set their properties to not copy to the output directory as below,

Load Multiple DLL as Single Exe in NET and NET Core

That’s all required for us to get started with coding for loading DLLs programmatically.

Here below is the complete code to achieve the same,

  class Program
    {

        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            Run();
        }

        static void Run()
        {
            Employee emp = new Employee();
            emp.Name = "TheCodeBuzz";
            Console.WriteLine(emp.Name);
        }

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("LoadMultipleDLL.Resources.ExternalApp1.dll"))
            {
                var assemblyData = new Byte[stream.Length];
                stream.Read(assemblyData, 0, assemblyData.Length);
                return Assembly.Load(assemblyData);
            }
        }
    }

Finally, build the code. Below it produces a single DLL with all resources embedded.

Load Multiple DLL as Single Exe in NET and NET Core

Now, this output can be executed directly or it can be added as a reference to other projects.

Load Multiple DLL as Single Exe in NET and NET Core

All references to all accessible classes defined in the embedded resources will be readily available to use in the target DLL or Application.

Other references :

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *