위의 답변은 정확합니다. 나는 단지 쪽지를 만들고 싶었다. Delegate/lambda에 대한 다른 참조를 유지하면 처리기로 사용 된 익명의 대리자는 구독을 취소 할 수 있습니다.
public event EventHandler MyEvent;
...
//adds a reference to this named method in the context of the current instance
MyEvent += Foo;
//Adds a reference to this anonymous function literal to MyEvent
MyEvent += (s,e) => Bar();
...
//The named method of the current instance will be the same reference
//as the named method.
MyEvent -= Foo;
//HOWEVER, even though this lambda is semantically equal to the anonymous handler,
//it is a different function literal and therefore a different reference,
//which will not match the anonymous handler.
MyEvent -= (s,e) => Bar();
var hasNoHandlers = MyEvent == null; //false
//To successfully unsubscribe a lambda, you have to keep a reference handy:
EventHandler myHandler = (s,e) => Bar();
MyEvent += myHandler;
...
//the variable holds the same reference we added to the event earlier,
//so THIS call will remove the handler.
MyEvent -= myHandler;
"약한 참조 이벤트"의 표준 양식/관용구가 있는가 : 람다는 "함수 리터럴"종류의 문자열 리터럴처럼 때문 평등을 결정할 때 그러나 문자열과는 달리 그들은 의미 비교되지입니까? –
@pst : 불행히도, 아니오. 이것은 이전에 논의 된 것입니다 (더 일반적으로 참조 할 수있는 대리인의 일부인 참조를 고려하지 않음). 그러나 복잡성과 잠재적 인 함정으로 인해 구현되지 못했습니다. 마지막으로 살펴본 이후로 계획에 변경이 있었을 수도 있지만, 내가 알기 엔 상황이 서서 계속 될 것입니다. WeakReference 클래스를 사용하는 솔루션조차도 문제를 다른 유형으로 옮깁니다. –