samedi 28 février 2015

Get methods with attribute and subscribe them to an event

How would one get all the methods that have a certain attribute applied to them through reflection, and then subscribe those methods to an event?


Class whose method is loaded through reflection:



public class EventsAndStuff
{
[DoStuffEvent]
public void OnDoStuff(object sender, DoStuffEventArgs e)
{
// Do Stuff
}
}


When the loading program starts, how would I subscribe OnDoStuff() to an event DoStuffEvent, so that when DoStuffEvent is invoked, it would call OnDoStuff and any other methods with a DoStuffEventAttribute applied?


I suspect this has to do with MethodInfo.CreateDelegate() but the MSDN documentation isn't particularly clear.


Most of the similar questions involve subsribing a non-reflection method to a reflection-loaded event. How could one do the reverse?






Aucun commentaire:

Enregistrer un commentaire