开发者

Unsubscribe .NET event from subscriber

开发者 https://www.devze.com 2023-03-25 15:21 出处:网络
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.

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

精彩评论

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

关注公众号