I
I
iRumba2015-03-25 15:53:06
.NET
iRumba, 2015-03-25 15:53:06

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

2 answer(s)
D
Daniil Basmanov, 2015-03-25
@iRumba

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;

S
Sergey Korenevsky, 2015-03-25
@Dier_Sergio_Great

EventHandler handler = (s, e) => MessageBox.Show("Woho");
button.Click += handler;
button.Click -= handler;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question