在单一应用程序中启动插件架构中的事件 [英] Firing events within a plug-in architecture vs single application

查看:167
本文介绍了在单一应用程序中启动插件架构中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发有关事件的插件架构。我可以在单个应用程序中做到这一点:(显然这是一个非常简化的版本,我正在尝试完成,如果这是我的代码,那么有更简单的方法可以实现这一点,只需尝试遵循逻辑);

I am getting stumped with my plug-in architecture that I am trying to develop with respect to events. I can do this just fine, in a single application: (Obviously this is a very simplified version of what I am trying to accomplish, and if this were my code there are easier ways to accomplish this, just try to follow the logic ;)

public Form1()
{
    public event EventHandler OnValueOver5Chars;

    Main()
    {
        OnValueOver5Chars+= new EventHandler(WarnUser);
        ....
    }

    private void textBox_Changed( object sender, EventArgs e )
    {
        if( sender.Text.count() > 5 )
            OnValueOver5Chars(sender, e); // WORKS HERE
    }

    private void WarnUser(object sender, EventArgs e)
    {
        ...
    }
}

然而,现在我有一个插件架构,其中插件实现了一个容纳我的事件的接口:

However, now I have a plug-in architecture, where the plugin implements an interface which houses my event:

// Interface.cs
public interface IPlugin
{
    event EventHandler OnValueOver5Chars;
    ...
}

// Plugin1.cs
public class Plugin1 : IPlugin
{
    public event EventHandler OnValueOver5Chars;

    Plugin1()
    {
        OnValueOver5Chars += new EventHandler(Plugin1WarnUser);
    }

    private void Plugin1WarnUser(object sender, EventArgs e)
    {
        ...
    }
}

// Form.cs
public class Form1 : Form
{
    public Form1()
    {
        Assembly SampleAssembly = Assembly.LoadFrom("Plugin1.dll");
        Type myType = SampleAssembly.GetTypes()[0];
        if (myType.GetInterfaces().Contains(typeof(IPlugin)))
        {
            IPlugin myInstance = Activator.CreateInstance(myType) as IPlugin;
            myInstance.OnValueOver5Chars(this, new EventArgs());
            // Compiler Error CS0079 - The event 'event' can only appear on the left hand side of += or -=
        }
}

????

推荐答案

C#有属性,它们不是直接作为定义在类之外的方法(或代表)。

Events in C# have the property that they are not "callable" directly as methods (or as such as delegates) outside of the class where they are defined.

在你的第一个例子中,你是从您定义它的类中调用事件。然而,在第二个例子中,您尝试从课外调用 OnValueOver5Chars ,因此出现错误。

In your first example you are calling the event from within the class in which you define it. In the second example, however, you are trying to call OnValueOver5Chars from outside the class - hence the error.

要解决这个问题,您可以考虑在 IPlugin 界面(例如 ValueOver5Chars )中添加一个方法,执行 OnValueOver5Chars 。请注意,将事件命名为 ValueOver5Chars (说),并提供一个方法 OnValueOver5Chars 来提升它(即相反的方式)。请参阅Windows窗体按钮课程及其单击事件。

To solve this you could consider adding a method to your IPlugin interface (e.g. ValueOver5Chars) that performs OnValueOver5Chars. Note that it is more common to name the event ValueOver5Chars (say), and provide a method OnValueOver5Chars to raise it (i.e. the other way round). See for example the Windows Forms Button class and its Click event.

这篇关于在单一应用程序中启动插件架构中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆