danijepg
danijepg

Reputation: 357

Events cleanup in c#

Let's say I have two objects A and B. A publish an event, and B is subscribed to this event. I know B can unsubscribe using the -= sintaxis, but is there any way to B to unlink from all events without going one by one?

I know A can iterate throught all clients using the GetInvocationList method, but I'm asking for B. I'm thinking in something like that as a way to ensure B code is not executed anymore due to an event and that I'm not forgiven any event.

thanks

Upvotes: 0

Views: 85

Answers (1)

Paulo Morgado
Paulo Morgado

Reputation: 14846

Although this is only a problem when B outlives A, it can happen.

The only way to do that is to keep track of all event subscriptions.

For the event provider, there's a class that easily manages subscribers: EventHandlerList. You would have to create your own for manage subscribed events.

Or you clould use the Reactive Extensions (Rx) to manage the event subscriptions.

Upvotes: 1

Related Questions