c# - Events: Get Event Observer from Observed -


I think how to collect supervisors of an incident from the class observed when using events and delegates.

An example of EventListener in this example is a list of ListWithChangedEvent, using the Vanilla Supervisor Patterns that will be easy to obtain, collection objects that are listening to ListWithChangedEvent (as That is to maintain the collection of supervisors anyway). However, by using events, it is a little less clear how the collection can be prepared according to the list of objects.

Is there something like this:

  has been changed. Choose GetInvocationList () (item = & gt; Items. Goal) .off type & lt; Eventilist & gt; (). ToList ();  

Looks suspicious, it looks a little complex.

EDIT: It compiles and gives me your answer. I just want to be a little careful that there is a bad way to do this.

You are just there, you have to be reminded that you can not use GetInvocationList () On the delegates marked with the event from outside the square, you must declare either a banking field or call from within the class.

  Category EventListener {public event publisher publisher = new event publisher (); Public EventListener () {publisher.Event + = HandleEvent; } Zero handel events (object sender, event ergges e) {}} class event publisher {public event event handler event; Public list & amp; Lt; Event Extension & gt; GetListeners () {Return Event.GetInvocationList (). Select (i = & gt; i.Target) .off type & lt; EventListener & gt; (). ToList (); }}  

Edit: If you are working with a third party category, you can not use a backing field or call GetInvocationList () in the classroom , Then you can use reflection:

  var fieldInfo = typeof (EventPublisher) .GetField ("Event", BindingFlags.Instance | BindingFlags.NonPublic); Var EventHandler = (EventHandler) Field Info.GetValue (listener.publisher); Var listeners = eventHandler.GetInvocationList (). Select (i => i.Target). Ollist ();  

Comments