Click here to Skip to main content
15,886,816 members
Articles / Programming Languages / C#

A Flexible Plugin System

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
3 Sep 2008LGPL34 min read 130.8K   1.8K   163  
A generic plugin system used to load and manage plugins
using System;
using System.Collections.Generic;
using ExampleApplication.Shared;
using ExamplePlugin.Shared;
using Fadd.Plugins;

namespace ExamplePlugin
{
    public class MyPlugin : IMyPlugin
    {
        private const string _pluginName = "MyPlugin";
        private readonly IPluginInfo _pluginInfo = new MyPluginInfo();
        private readonly IEnumerable<string> _dependencies = new List<string>();

        /// <summary>
        /// This name is used to determine dependencies, should always be in english. 
        /// Should not be confused with the human friendly name in <see cref="IPlugin.PluginInfo"/>.
        /// </summary>
        public string PluginName
        {
            get { return _pluginName; }
        }

        /// <summary>
        /// Information about the plugin.
        /// </summary>
        public IPluginInfo PluginInfo
        {
            get { return _pluginInfo; }
        }

        /// <summary>
        /// Other plugins that this one depends on. The list should contain <see cref="PluginName"/>s.
        /// </summary>
        /// <value>Is never null</value>
        public IEnumerable<string> Dependencies
        {
            get { return _dependencies; }
        }

        /// <summary>
        /// Start the plugin
        /// </summary>
        /// <param name="application">Application interface exposed towards the plugins.</param>
        public void Start(IApplication application)
        {
            IMyApplication myApplication = (IMyApplication) application;
            myApplication.RegisterComponent<IWorldPeace>(new WorldPeaceThroughLove());
            myApplication.AddMenuItem("Click me", OnClick);
        }

        /// <summary>
        /// Invoked when the user clicks on our menu item
        /// </summary>
        /// <param name="name"></param>
        private void OnClick(string name)
        {
            Console.WriteLine("Omg! You clicked me!");
        }

        /// <summary>
        /// Function that must be implemented
        /// </summary>
        /// <param name="name"></param>
        public void SayHelloTo(string name)
        {
            Console.WriteLine("Hello " + name);
        }
    }
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Founder 1TCompany AB
Sweden Sweden

Comments and Discussions