Click here to Skip to main content
15,891,204 members
Articles / Security

A simple plug-in engine using Reflection

Rate me:
Please Sign up or sign in to vote.
4.91/5 (22 votes)
26 Sep 2012CPOL11 min read 66.4K   1.1K   95  
This article describes how to create and use configurable plug-ins in your application.
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Security;
using System.Security.Permissions;

namespace PluginDemo
{
    internal class Program
    {
        // Directory that contains plugins files
        private const string pluginDir = "Plugins";

        // Plugins files extension mask
        private const string pluginExtMask = "*.dll";

        static void Main(string[] args)
        {
            Console.WriteLine("1. Old way of running the plugins - full trust:\r\n");
            var pluginMan = new PluginBase.PluginManager();
            RunPlugins(pluginMan);

            Console.WriteLine(Environment.NewLine);

            Console.WriteLine("2. New way of running the plugins - limited trust:\r\n");
            var pset = new PermissionSet(PermissionState.None); /* deny all */
            /* uncomment the next line to allow file operations for all the plugins  */
            // pset.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
            /* uncomment the next line to allow working of the WebDownloadPlugin */
            // pset.AddPermission(new WebPermission(PermissionState.Unrestricted));
            pluginMan = PluginBase.PluginManager.GetInstance(pset);
            RunPlugins(pluginMan);

            Console.ReadKey();
        }

        static void RunPlugins(PluginBase.PluginManager pluginMan)
        {
            var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), pluginDir);

            foreach (var f in new DirectoryInfo(path).GetFiles(pluginExtMask))
            {
                var plugin = pluginMan.LoadPlugin(Path.Combine(path, f.Name));
                try
                {
                    plugin.Run();
                    Console.WriteLine("Plugin {0} has finished work\r\n", plugin.Name);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An exception occurred in the {0} plugin: {1}\r\n", plugin.Name, ex.Message);
                }
            }
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions