4

I have a class with a method, Register that subscribes to a number of events on classes that it contains, using the standard aClass.SomeEvent += the_handler. This class also has an Unregister method that unsubscribes from these events using -=. This works just fine but we're finding that if we add a new event to subscribe to that it's very easy to forget to include the unsubscription in Unregister. This manual method of maintaining event subscriptions is proving to be fragile.

Is there a way to maintain a list of subscriptions that can be iterated over and unsubscribed from dynamically? (And potentially iterate over and re-subscribe when calling Register after Unregister).

Some details: The class has a reference to 3 other classes (currently, but not definitively limited to 3), the various events on these classes are all of type EventHandler or EventHandler<T>.

RichK
  • 11,318
  • 6
  • 35
  • 49

2 Answers2

3

how about getting invocation list from the EventHandler.GetInvocationList() and then ierate through and manually remove/unregister them ? note, you only have access to the GetInvationList() method from the class that has that EventHandler, so you might need to expose a method UnregisterAll() to make sure it removes all the delagates in the event invocation list

you can also make sure your class inherits IDisposable and with using(){ } it will call Dispose which will clean up all subscribers

Bek Raupov
  • 3,782
  • 3
  • 24
  • 42
  • Thanks for the answer, I too was thinking about iterating the invocation list in each class but I couldn't see how this will work. The events are/can also be subscribed to by other classes. How can I iterate through the list and only remove invocations from my Register class? – RichK May 13 '11 at 11:26
  • It also doesn't seem like the responsibility of those classes to unhook the events. Maybe if I can expose the invocation list so the Register class can see it, but even then I don't know how to programatically unsubscribe from an event by iterating a list of delegates. The -= won't work because I won't be able to tell it which method to unhook. Nightmare. – RichK May 13 '11 at 11:27
  • 1
    yeah, that's the tricky onw, if you want to remove/unsubscribe just for your class and not other class. can't think of better way then using reflection, or redesign your whole subscription process maybe (i.e. not to use EventHandler but have your own event handler, which keeps track of specific classes and subscriptions). EventHandler wraps up event so that you can use -= and += easily. maybe you need to override that – Bek Raupov May 13 '11 at 12:16
0

After a thorough look through SO I found this answer:

C# Dynamic Event Subscription

That does what I want (almost). I don't like having to name events using strings as and such I won't be pursuing this design any further. Even though it's not the design I want, the answer shows a very useful method to achieve the desired behaviour and as such I'm marking this as accepted.

Community
  • 1
  • 1
RichK
  • 11,318
  • 6
  • 35
  • 49