且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在单一应用程序中启动插件架构中的事件

更新时间:2023-11-22 17:27:46

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.