Possible Duplicate:
Is it possible to subscribe to event subscriptions in C#?
Searching for information about event events isn’t easy. I’m just hoping that it’s not because it can’t be done. I’m just hoping that the answer is “there’s an event for that,” or some other means.
I’m dealing with an event where listening for the event is comparatively expensive. (A watch has to be put upon it in another system which I’m tying into, involving keeping track of which things to keep track of and COM overhead.)
Therefore, I don’t really want to be watching for the event unless I know that something wants it.
The simple way of doing it is like this:
public class MyThing
{
public delegate void MyEventHandler(object sender, MyEventArgs ea);
public event MyEventHandler Change;
public void StartWatching()
{
...
}
public void StopWatching()
{
...
}
}
This is used in this manner:
var thing = new MyThing();
thing.Change += this.thing_Change
thing.StartWatching();
thing.Change -= this.thing_Change
thing.StopWatching(); // Hopefully nothing else is watching...
But this isn’t as elegant as I’d like. I could do it with replacing thing.Change += x with thing.StartWatching(x), which would then start watching, and the converse would check if Change == null, but I’d like it if it could be done with the full elegance of the event model.
Sounds like a custom event accessor might be what you are looking for.