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
精彩评论