Answer the question
In order to leave comments, you need to log in
How to unsubscribe from an event if the subscription is a lambda expression?
It would seem that everything is simple, we create an instance based on the lambda expression and subscribe it to the event, and also unsubscribe, BUT. I need this construction
Object.Event += (sender,e) =>
{
sender.Event -= ...... ;
}
How can I do this without explicitly creating a delegate?
After all, is it possible to get a function handle from the function itself, like this inside an object, to get a handle to this object?
Answer the question
In order to leave comments, you need to log in
Keep a reference to the anonymous method.
public delegate void TestEventHandler(object sender, object e);
public event TestEventHandler TestEvent;
TestEventHandler test = null;
test = (sender, e) => { TestEvent -= test; };
TestEvent += test;
EventHandler handler = (s, e) => MessageBox.Show("Woho");
button.Click += handler;
button.Click -= handler;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question