开发者

Unsubscribe .NET event from subscriber

I was asked a very interesting question about events. I think the answer is no but I'm curious if there is a way that I don't know of.

in the next code sample, can I delete the subscription from the referring class开发者_JS百科 B or its Method X??

Class1 A = new Class1();
Class2 B = new Class2();

A.DoneIt += B.X;

meaning doing A.DoneIt -= B.X; without any access to Class A(nor by reflection on class A).


No, you can only unsubscribe if you can access the event.

Delegates point one way, i.e. to a method. The method has no pointer back to the event. So the only access to the event to unsubscribe is through the type instance it's defined on.

Further reading: Events And Delegates

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜