I have a method that, throughout the lifetime of my app, gets added to any number of events. At some point I'd like to remove the method from all of the events that reference it. I only know the method...I would love to NOT keep a local List<EventHandler>
of all the events the method gets added to because I think that somewhere/somehow the CLR is doing this for me for the purposes of garbage collection. Some pseudo-code below, any ideas? Thanks...
What I want to do:
public class Subscriber
{
public virtual void EventRaised(object pSender, EventArgs pArguments)
{
MessageBox.Show("EventRaised");
}
public virtual void UnsubscribeAll()
{
//Something like:
EventRaised.RemoveReferences();
//or
foreach(var MyReference in EventRaised.References)
MyReference.Remove();
}
}
var MySubscriber = new Subscriber();
ThisEvent += MySubscriber.EventRaised;
ThatEvent += MySubscriber.EventRaised;
//on and on...
MySubscriber.UnsubscribeAll();
What I don't want to do:
public class Subscriber
{
protected List<EventHandler> Publishers = new List<EventHandler>();
public virtual void EventRaised(object pSender, EventArgs pArguments)
{
MessageBox.Show("EventRaised");
}
public virtual void Subscribe(EventHandler pPublisher)
{
pPublisher += EventRaised;
Publishers.Add(pPublisher);
}
public virtual void UnsubscribeAll()
{
foreach(var Publisher in Publishers)
Publisher -= EventRaised;
Publishers.Clear();
}
}
var MySubscriber = new Subscriber();
MySubscriber.Subscribe(ThisEvent);
MySubscriber.Subscribe(ThatEvent);
//on and on...
MySubscriber.UnsubscribeAll();
Another Example
If the CLR doesn't keep track of the references per Stephen Cleary - bummer...that forces me into plan B. The answers and comments don't quite fit my problem - it's my fault though so I'll add another example below. JBall's code example #1 is very interesting and I'll add that to my bag of tricks.
I'm not removing the event references to the method with the need to or intention of restoring them later...I just want them gone. How about this pseudo-code - from the viewpoint of the Child, I want to say "forget whatever events my Parent attached to and attach to these events instead." I have several workarounds, including keeping a List<>
of attached events, but I was hoping to leverage work that the CLR does for me behind-the-scenes. An added benefit of a CLR-maintained structure would've been the fact that it's definitive and final...there wouldn't have been a chance of attaching a method to an event without actually recording the reference.
public class Parent
{
public Parent()
{
AnObject.ThisEvent += EventRaised;
AnObject.ThatEvent += EventRaised;
}
public virtual void EventRaised(object pSender, EventArgs pArguments)
{
MessageBox.Show("EventRaised");
}
}
public class Child
{
public Child(): base()
{
//What I want to do:
EventRaised.RemoveAllEventReferences();
AnotherObject.ThisEvent += EventRaised;
AnotherObject.ThatEvent += EventRaised;
}
}