Click here to Skip to main content
12,697,190 members (27,154 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

39.1K views
961 downloads
97 bookmarked
Posted

A simple plug-in engine using Reflection

, 26 Sep 2012 CPOL
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)

Share

About the Author

Vitaly Zhukov
Software Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 27 Sep 2012
Article Copyright 2012 by Vitaly Zhukov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid