custom plugin questions!

Hi there.

I have no idea but had a sudden urge to ask about things like this, I guess its good for the knowledge!

I have an app created in .NET 1.1

I *may* wish to implement plugin's with the application, so developers can create their own plugin's for the application.

Question 1) If a plugin was created in .NET 2.0, will it still work with the .NET 1.1 application (obviously assuming they have both frameworks installed)

Question 2) How would one go about creating plugin's for their application

Question 3) Is it possible for this plugin to implement/"subscribe" to public events exposed by my application, so when I raise an event, any of the plugin's who have a subscription to the event, is notified

That's it for now :)

Many thanks!




Answer this question

custom plugin questions!

  • Spenceee

    Thanks! :)

  • awj100

    ok I have some more Q's

    how can I hook/raise an event to the plugin's that have been loaded

    the plugin's that have been loaded are in a collection.

    each plugin implements/includes an interface IPlugin.

    IPlugin has a few delegates/events

    When loading/instantiating the plugin's, how can I initialize/establish events, so if I raise an event in my application, which is included in the interface, that it will call/raise the events to the other plugins that have been loaded



  • Nawar G.

    You can have some object in you app implement an interface that you expose to your plugin. I called this interface IApplication. This is the protocol used by your plug-ins to talk to the application. Of course you have to expose this object somehow to the plug-in. One idea is to pass it as parameter to IPlugin.Initialize after creating the plugin. In IPlugin.Initialize the plugin can use this interface to subscribe to any event exposed but the application. The code I provided is just a sketch, but it runs. You can try to step through it in the debugger to see what happens.
  • pc0416

    I hope I can help answer some of your questions or point you in the right direction.

    Question 1:

    I do not believe that plugins would work with mixing 1.1 and 2.0. I am not exactly sure on this as I have not tried it but that is my guess until I try it.

    Question 2 and 3:

    One of the easiest ways to support pluggins is by implementing Interfaces. For example, you could implement an IPlugin interface that describes events, methods, and properties the plugins must implement. This is where Polymorphism is at its best. Here is an example. You could create an interface that defines the following methods and events:

    Interface IPlugin

    event Remove_Item(sender as Object, e as eventargs)

    sub DisplayMessagebox(text as string)

    End Interface

    Then you would require all of your plugins to implement this interface. Your main application could then use reflection or Activator.Createinstance to create an instance of your plugin and then call the corresponding methods or react to the events. There are a few different articles out there describing in much more detail the specifics but this is the general idea that can be used to create a plugin architecture. I hope this helps.


  • Mark Dooley

    Thank-you

    I do apologize, still trying to grasp this.

    I think I have everything except for the implementation of events/delegates.

    interface:



    public delegate void SomeMessage(string theMessage);
    public interface IPlugin
    {
    string PluginDescription { get; }
    void DoInitialize();
    event SomeMessage OnEventSomeMessage;
    }

    a plugin



    //implements the interface IPlugin
    void DoInitialize()
    {
    //what do I need to do here for creating the events in this plugin
    }

    MainApp



    public delegate void SomeMessage(string theMessage);
    public event SomeMessage OnEventSomeMessage;
    ....
    this.OnEventSomeMessage += new SomeMessage(MainForm_OnEventSomeMessage);
    ....
    //loading plugins
    IPlugin thePlugin = (IPlugin)Activator.CreateInstance(theTypeOfPlugin);
    //here, how do I wire up/create the events of the plugins to "link" up to this main application
    thePlugin.DoInitialize();

    this.theCollectionOfPlugins.Add(thePlugin);

    I hope that makes sense. I have been following you're generous code however somethings I don't quite understand, like the IApplication interface and such.

    I hope you can help me using this code I have about where to place the events, how to make them work/instantiated and how to call the events for each plugin I have loaded, and placed in the collection.



  • Rudemusik

    Many thanks, yes I had eventually got that there had to be a "link" to the plugin for it to "talk" to the main app.

    I still cannot seem to get the events to be hooked up to the main app (host)



  • Patrick Sears

    namespace Program

    {

    public interface IApplication

    {

    event EventHandler ApplicationEvent;

    }

    public class Application : IApplication

    {

    #region IApplication Members

    public event EventHandler ApplicationEvent;

    #endregion

    public void FireEvent()

    {

    ApplicationEvent(this, EventArgs.Empty);

    }

    }

    public interface IPlugin

    {

    void Initialize(IApplication application);

    }

    public class APlugin : IPlugin

    {

    #region IPlugin Members

    public void Initialize(IApplication application)

    {

    application.ApplicationEvent += new EventHandler(OnApplicationEvent);

    }

    void OnApplicationEvent(object sender, EventArgs e)

    {

    Console.WriteLine("Handled application event.");

    }

    #endregion

    }

    public class Program

    {

    static void Main()

    {

    Application app = new Application();

    IPlugin plugin = (IPlugin) Activator.CreateInstance(Type.GetType("Program.APlugin"));

    plugin.Initialize(app);

    app.FireEvent();

    }

    }

    }


  • KnobCreek

    Thanks! I was aware about interfaces and so on but never actually used it.

    cool! you have been most helpful!



  • Mitch Walker - MSFT

    The CLR 1.1 cannot run 2.0 assemblies. There is only one instance of the CLR inside a process, so obviously only one version. Check an example: http://msdn.microsoft.com/library/ url=/library/en-us/dnaspp/html/pluginframework.asp frame=true
  • Timeware

    You can have your application expose a "container" interface to your plug-ins (probably defined in the same assembly as IPlugin). So everytime you create a new plugin you can call an Initialize method of IPlugin, passing the container interface as parameter. This container interface would have as members all the events you want to provide to the plug-ins. Inside Initialize the plug-ins can subscribe to the events they are interested in.
  • tomjohnr

    Thanks.

    is there any chance of a sample/example as I am new to this stuff :)



  • custom plugin questions!