c# - How to make an event which gets triggered in another class -


Event PluginEvent is triggered when a plugin is added to my code for PluginManager I also want to get the events triggered in the test class . Somehow I can not solve this problem, the event has only begun in the PluginManager class . I read some articles about how to create events and so on, but I got more confused


PluginManager class

  public Square PluginEventArgs {public PluginEventArgs (strings) {text = s; } Public string text {get; Private set; } // readonly} public class PluginManager {// Declare Delegate (while using non-normal pattern). Public Representative Zero PluginEventHandler (Object Sender, PluginEventArgs E); // Announce Event Public Event PluginEventHandler PluginEvent; Protected Virtual Zero RaiseSampleEvent (String Message) {If PluginEvent! =) PluginEvent (This, New PluginEventArgs (Message)); } Public Plugin Manager () {PluginEvent + = PluginManager_PluginEvent; SomeMethod (); } Zero PluginManager_PluginEvent (Object Sender, PluginEventArgs E) {// Triggered this event =}} Public Zero someMethod () {// Code RaiseSampleEvent ("Plugin Name"); // test}}  

My test class:

  class test {public trial} {PluginManager pluginMg = New PluginManager ( ); PluginMg.PluginEvent + = pluginMg_PluginEvent; } // I want this event to be triggered when a new plugin is implemented by Zero PluginMugP pluginEvent (Object Sender, PluginEventArgs E) {MessageBox.Show (e.Text); }}  

How do I manage to get an event that triggers a test class ? Thank you for any advice!

You are actually doing anything other than a logical mistake.

In your test class you are making the plugin manager using Constructor. Manufacturers of PluginManager subscribe to the first event and then lift it.

After that you are subscribing to that event.

The simple problem is that when you are taking an event in your testing class has not subscribed yet. When you prepare that incident again, then everything should be done properly.

One more thing is that instead of creating your own representatives, you will use the generic event handler class, it keeps your code cleaner and everyone knows that this is an event at first glance.

Just get the assurance of PlugInEventArgs from EventArgs and then use EventHandler.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -