Creating plugins in C#: The handler (Part 2/4)

Welcome to the second part of our little tutorial ^.^

In this part I’ll explain how the handler works and what you need in order for it to work…

The handler

The handler consists out of 2 things:

  • An interface
  • A class that extends the Attribute class

Add a new C# Class Library project and name it PluginHandler.

Delete the default class that gets created (Class1.cs)

The interface

Add a new Interface named IPlugin (Add > New Item > Interface)

Adding a new interface

The reason we include the interface, is because any plugin you make must implement the interface and needs it in order to be shown.
Since this is just a basic explanation of how it works, I’m only going to show you what you need in order to load your plugin.

Interface source code:

// Plugin interface for the plugin handler
using System;

namespace PluginHandler
{
    public interface IPlugin
    {
        void ShowPlugin();
    }
}

The class

Now that we have our interface, we add a new class called PluginAttribute: (Add > New Item > Class)

Adding a new class

Class source code:

// Plugin class that extends the attribute class
using System;

namespace PluginHandler
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public sealed class PluginAttribute : Attribute { }
}

Build

Now that we have added the 2 objects and modified their code, it’s time to build it.

Build > Build PluginHandler


Continue to the next part of this tutorial…

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.