Вы можете использовать MethodInfo.GetCurrentMethod
внутри своей лямбды, чтобы получить MethodInfo лямбды.
С MethodInfo вы можете использовать Delegate.CreateDelegate
, чтобы получить правильно набранного делегата, представляющего вашу лямбду.
И с помощью делегата вы можете отменить регистрацию лямбда-выражения, не сохраняя свою функцию в переменной или не называя ее именованным методом.
class MyClass
{
public event EventHandler TheEvent;
void TestIt()
{
TheEvent += (sender, eventargs) =>
{
Console.WriteLine("Handled!"); // do something in the handler
// get a delegate representing this anonymous function we are in
var fn = (EventHandler)Delegate.CreateDelegate(
typeof(EventHandler), sender,
(MethodInfo)MethodInfo.GetCurrentMethod());
// unregister this lambda when it is run
TheEvent -= fn;
};
// first time around this will output a line to the console
TheEvent(this, EventArgs.Empty);
// second time around there are no handlers attached and it will throw a NullReferenceException
TheEvent(this, EventArgs.Empty);
}
}