Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically load DLL files in C# project - how?

Tags:

c#

plugins

dll

In my project I need to use plugins. But to use these in my project I need to import an reference of the plugin. Since I can't know how many or which plugins the project uses beforehand I would like to import them dynamically in my project.

    String path = Application.StartupPath;
    string[] pluginFiles = Directory.GetFiles(path, "*.dll");
    ipi = new IPlugin[pluginFiles.Length];
    Assembly asm;

        for (int i = 0; i < pluginFiles.Length; i++)
        {
            string args = pluginFiles[i].Substring(
                pluginFiles[i].LastIndexOf("\\") + 1,
                pluginFiles[i].IndexOf(".dll") -
                pluginFiles[i].LastIndexOf("\\") - 1);

            asm = Assembly.LoadFile(pluginFiles[i]);
            Type[] types = asm.GetTypes();

In this code example I searched all the .dll files and put them into a string list. But how can I now load all these .dll files? Or is there a way to use these .dll files without really importing them?

like image 773
Nanou Ponette Avatar asked Nov 06 '12 14:11

Nanou Ponette


1 Answers

The MEF (Managed Extensibility Framework) Method:

You'll want to add references to System.ComponentModel.Composition to your projects that utilize the import/export functionality of MEF.

First, the bootstrapper/loader (in my case, I just added it to the Main class).

Program.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using MEFContract;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            var prgm = new Program();

            // Search the "Plugins" subdirectory for assemblies that match the imports.
            var catalog = new DirectoryCatalog("Plugins");
            using (var container = new CompositionContainer(catalog))
            {
                // Match Imports in "prgm" object with corresponding exports in all catalogs in the container
                container.ComposeParts(prgm);
            }

            prgm.DoStuff();

            Console.Read();
        }

        private void DoStuff()
        {
            foreach (var plugin in Plugins)
                plugin.DoPluginStuff();
        }

        [ImportMany] // This is a signal to the MEF framework to load all matching exported assemblies.
        private IEnumerable<IPlugin> Plugins { get; set; }
    }
}

The IPlugin interface is the contract between the imports & exports. All plugins will implement this interface. The contract is pretty simple:

IPlugin.cs:

namespace MEFContract
{
    public interface IPlugin
    {
        void DoPluginStuff();
    }
}

Finally, you can create as many plugins as you like in different assemblies. They must implement the contract interface and also be decorated with the "Export" attribute to indicate to MEF that they should be matched up with any corresponding imports. Then drop the dlls in a "Plugins" folder (this folder should reside in the same location as the executable). Here's a sample plugin:

Plugin.cs:

using System;
using System.ComponentModel.Composition;
using MEFContract;

namespace Plugin
{
    [Export(typeof(IPlugin))]
    public class Plugin : IPlugin
    {
        public void DoPluginStuff()
        {
            Console.WriteLine("Doing my thing!");
        }
    }
}
like image 74
Cole Cameron Avatar answered Oct 09 '22 11:10

Cole Cameron