Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove handlers on disposing object

Tags:

c#

.net

I can think of a few messy ways to solve this, but it strikes me that there should be a far more elegant solution than those which I've already come up with.

What's the most appropriate way for an object to cleanse itself of all of its event handlers prior to being disposed. It's a shame the event handler can't be enumerated upon.

In theory, is it considered more correct for the code adding the handler to an object to remember to remove it than assuming the object will clean itself up before it goes out of scope?

like image 371
Jesse Hallam Avatar asked Jan 20 '09 09:01

Jesse Hallam


1 Answers

There is a way to avoid this common problem with events - WeakEvent pattern.

like image 66
aku Avatar answered Sep 27 '22 20:09

aku