mardi 24 février 2015

C# Delegate and Events for Beginners

I am a beginner C# programmer just stepping into the advanced world of plugins.


What I have at the moment: I have a base architecture for plugins to provide GUIs, Functions, and classes and each instance of GUI, function and class can be accessed by other plugins. (For ex. if the state of one plugin (plugin a) changes because the user has changed then it can change the text in a label in plugin b to the new user name).


What I am trying to get working: In one word. Events. If plugin a has an event I want plugin b to be able to subscribe to it. I am using a generic event handler delegate but it doesn't want to subscribe.


Resources I have seen already: http://ift.tt/1D9LpT5 - Extremely useful


c# Plugin Event Handling


Firing events within a plug-in architecture vs single application


The issue is that most people are trying to subscribe to an event in a plugin from an application. I wish to subscribe one plugin to the event of a second plugin


My code at the moment:


Plugin 1 Events:



public class Events
{

public event tester testingevt;
public delegate void tester(object o, EventArgs e);

public void fireIt()
{
if (testingevt != null)
{
testingevt(this, null);
}
}
}


Plugin 2 (subscription):



public void onLoad()
{
object tempInstance = pf.getInstance("thisisaplugin", "Events");
Type t = tempInstance.GetType();
t.GetEvent("testingevt").AddEventHandler(tempInstance, new EventHandler(tester));
}


I have also seen various MSDN articles but none of them try to subscribe one plugin to another.


The code I am using came directly from the dreamincode.net link.


I have tried many different ways by creating delegate types, taking eventinfo variables to store the event and this was the closest I got to gathering it but the error this code throws is:



Object of type 'System.EventHandler' cannot be converted to type 'Test_Plugin_for_Server.Events+tester'



Please can anyone help me out?


Thanks in advance.






Aucun commentaire:

Enregistrer un commentaire