简体   繁体   中英

C# Communication between plugin and host application

I am writing plugin based application.

Host application:

namespace CSK
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {                  
        InitializeComponent();
        LoadPlugins();

    }

    public void LoadPlugins()
    {
        DirectoryInfo di = new DirectoryInfo("./Plugins");

        foreach (FileInfo fi in di.GetFiles("*_Plugin.dll"))
        {
            Assembly pluginAssembly = Assembly.LoadFrom(fi.FullName);

            foreach (Type pluginType in pluginAssembly.GetExportedTypes())
            {

                if (pluginType.GetInterface(typeof(MainInterface.PluginHostInterface).Name) != null)
                {
                    MainInterface.PluginHostInterface TypeLoadedFromPlugin = (MainInterface.PluginHostInterface)Activator.CreateInstance(pluginType);
                    MainInterface.IMother mother = new ApiMethods(this);
                    TypeLoadedFromPlugin.Initialize(mother);
                }

            }
        }
    }
}

Interface:

namespace MainInterface
{
public interface PluginHostInterface
{
    void Initialize(IMother mother);
}

public interface IMother
{
    MenuItem addMenuItem(String header, String name);
    MenuItem addSubMenuItem(MenuItem menu, String header, String name);
    Boolean receiveMessage(String message, String from);
    Boolean addContact(String name, String status, String proto, String avatar = "av");
}
}

Test plugin:

namespace Plugin_Test
{
public class MainClass : MainInterface.PluginHostInterface
{
    private MainInterface.IMother CSK;

    public void Initialize(MainInterface.IMother mainAppHandler)
    {
        CSK = mainAppHandler;

    }

}
}

And now, i want to execute some methods in Plugin Test from my host application. Of course, there will be many plugins, and not every of them will contain specified methods. I was trying to use events but with no success. Any idea how to do it?

Class with Events:

public class EventProvidingClass {

    public event EventHandler SomeEvent;

    public void InvokeSomeEvent() {
        if(SomeEvent != null) SomeEvent.Invoke(this, new EventArgs());
    }

}

Your Plugin Interface:

public interface PluginHostInterface
{
    void Initialize(IMother mother);
    void InitializeEvents(EventProvidingClass eventProvider);
}

Plugin Class:

public class MainClass : MainInterface.PluginHostInterface
{
    private MainInterface.IMother CSK;

    public void Initialize(MainInterface.IMother mainAppHandler)
    {
        CSK = mainAppHandler;
    }

    public void InitializeEvents(EventProvidingClass eventProvider)
    {
        eventProvider.SomeEvent += someEventHandler;
    }

    private void someEventHandler(object sender, EventArgs e)
    {

    }
}

and then call InitializeEvents after the Initialize function. Of course you can put the events where you want them, you just need to make sure that you make them available for the Plugin so that the Plugin can assign its EventHandlers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM