tags:

views:

171

answers:

2

Hi everybody!

I am kind of new to C# .Net and I am enjoying the learning process! Right now I am working on a project (just to learn) in which plug-ins are the main key.

I did the following till now which seems to work fine:

Interface:

namespace My.test
{
    public interface IMyPlugin
    {
        bool Execute(string Id, Object param);
        List<string> GetPluginIdList();
    }
}

a test plug-in implementing IMyPlugin Interface:

using System;
..
..
using My.test;

namespace demoplg
{
    public class SamplePlugIn: IMyPlugin
    {

        public bool Execute(string Id, object param)
        {
            MessageBox.Show(Id, "SamplePlugIn");
            return false;
        }

        public List<string> GetPluginIdList()
        {
            List<string> result = new List<string>();
            result.Add("test1");
            result.Add("test2");
            return result;
        }
    }
}

and in the main application (Host) I am using Reflections to load the Dll (assembly) and create an Instance of the plug-in.

try
            {
                Type[] types = PlugIn.GetTypes();
                Type MyPlugInType = null;
                foreach (Type libtype in types)
                {
                    //check if it implements the IMyPlugin.
                    Type[] interfacelist = libtype.GetInterfaces();
                    foreach (Type lookupInf in interfacelist)
                    {
                        if (lookupInf.Name.Equals("IMyPlugin"))
                        {
                            MyPlugInType = libtype;
                            break;
                        }
                    }

                    if (MyPlugInType == null) continue;

                    IMyPlugin xplg = null;
                    ConstructorInfo ci = MyPlugInType.GetConstructor(new Type[] { });
                    xplg = ci.Invoke(null) as IMyPlugin;
                    return xplg;
                }
                return null;
            }
            catch
            {
                return null;
            }

well, now what I want to have is, that a plug-in can have it's on configuration GUI, which I would like to add that in my Host (Main application) by calling something for example: xplg.RegisterPluginGUI();

Let say something in the form of a tree-view on the left side showing the name/description of the plug-in, and on the right side kind of a container where I can drop the UI of the plug-in on it.

can some one please give some advice how to achieve this?

Well, I've done something similar in Delphi which is not a very clean work ;-) Using WIN API to process the messages of a from inside a dll cached in the main form!!!

Thanks in advance! AK

A: 

You could add to your interface IMyPlugin a method or a property which returns the configuration control:

public interface IMyPlugin
{
    UserControl GetConfigurationControl();
}

public class SamplePlugIn: IMyPlugin
{
    public UserControl GetConfigurationControl()
    {
        return new MyConfigurationControl();
    }
}

public class MyConfigurationControl : UserControl
{
}

After that, you can use the user control in a form in your application.

If you prefer to use a form, another option is:

public interface IMyPlugin
{
    void ConfigurePlugin();
}

public class SamplePlugIn: IMyPlugin
{
    public void ConfigurePlugin()
    {
        using(YourPluginConfigurationForm dlg = new YourPluginConfigurationForm())
        {
             dlg.ShowDialog();
        }
    }
}
Francis B.
Thanks! The Form idia is simple and IMHO the easiest to Implement.The UserControl idia is interesting and I've to give a try to see if I can still use for example TAB key to jump from one control to the other between the Host control and the Plug-in Controls.
Gohlool
+1  A: 

It might be a little offtopic but I would recommend you to learn following libraries for plugins development instead of creating your own:

Managed Extensibility Framework

System.AddIn Namespace

If your goal is just learning then your code might be OK, but in real-world scenarios you should use proven solutions like those mentioned above.

Also I want to mention that MEF is open-source project and you can possibly find answers on many questions regarding plugins architecture.

aku
Thank you for the links! I'll check them for sure!
Gohlool
+1 for mention of MEF. Using something pre-built is a good idea if there's a good match as it eases maintenance.
Jeremy McGee