Gohlool
Gohlool

Reputation: 463

Help me please with writing a Plug-in in C# which exports it's on let say Config GUI

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

Upvotes: 1

Views: 396

Answers (2)

aku
aku

Reputation: 123966

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.

Upvotes: 1

Francis B.
Francis B.

Reputation: 7208

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();
        }
    }
}

Upvotes: 0

Related Questions