I know a lot of people have asked the question of "how do I unsubscribe the following"
myButton.Click += (s, e) => MessageBox.Show("Hello World!");
With the obvious answer of
EventHandler HelloWorld = delegate { MessageBox.Show("Hello World!"); };
myButton.Click -= HelloWorld;
myButton.Click += HelloWorld;
But what I'm using a lambda to create a closure? What if my object has an event called AssessmentRationChanged
that is of type Action
, and I'm wiring it thusly:
foreach (MassFMVUpdateDTO dto in CurrentProperties)
dto.AssessmentRationChanged += () => setCellColorBasedOnAssessmentRatioValue(dto);
What if there's a chance I've already set this handler for some / all of the objects in this loop? Is there a way to unsubscribe them?
I'm sure I could use reflection and clear the handler completely, but is there a cleaner way?
No, you have to store the references to the delegates, basically.
Remember everything that you'll want to unsubscribe later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With