Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / Windows Forms

PluginManager - A C# utility to load plug-in based components

Rate me:
Please Sign up or sign in to vote.
4.72/5 (11 votes)
3 Dec 2008CC (ASA 2.5)9 min read 64.9K   2.7K   150  
PluginManager is a simple desktop utility that loads and runs simple control based plug-ins.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using Citrus.Forms.PluginManager.View;
using System.Reflection;
using Citrus.Forms.PluginManager.Controller;
using Citrus.Forms.PluginManager.Model;
using System.IO;

namespace Citrus.Forms.PluginManager.View
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new PluginManager());
        }

        /// <summary>
        /// The following allows the plugin manager to load external assemblies
        /// that are referenced by plugins. These plugins are assumed to be
        /// in the plugins folder, and is resolved after the CLR heuristics
        /// for loading a assembly fails.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            PluginStore pluginStore = PluginStoreManager.LoadPuginStore();
            if (null == pluginStore) return null;

            int index = args.Name.IndexOf(',');
            string assemblyName;
            if (index > 0)
            {
                assemblyName = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";
            }
            else
                assemblyName = args.Name + ".dll";

            foreach (PluginInfo plugin in pluginStore.Plugins)
            {
                string assemblyFile = Path.Combine(plugin.InstallPath, assemblyName);
                assemblyFile = EnvironmentSettings.GetFullPath(Path.Combine(plugin.InstallPath, assemblyName));
                if (File.Exists(assemblyFile))
                {
                    return Assembly.LoadFile(assemblyFile);
                }
            }
            return null;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Web Developer
United Kingdom United Kingdom
I work as a Technology Lead for an IT services company based in India.

Passions include programming methodologies, compiler theory, cartooning and calligraphy.

Comments and Discussions