2

I have 3 objects. Object A has an event X that other objects can subscribe to. Object B registers for Object A's X event.

How do I deregister Object B from Object A's X event from within Object C?

1 Answer 1

1

You don't - the point of events is that it encapsulates the pub/sub part so that no-one else can mess.

Now if you (object C) can act as a proxy for object B the whole time, that's a different matter. If you expose your own event which object B subscribes to instead of it subscribing to object A - and if you subscribe to object A on behalf of object B, then there are various ways of unsubscribing from object A. But unless you've been involved in that process, what "right" have you to mess with another piece of code's event handlers?

EDIT: Just to pick up on your reflection point... if you have the appropriate permissions then you may be able to find a field which backs the event. At that point you can examine it directly - but that still doesn't mean you know which handlers have been added by Object B. You could find handler methods that are within the type object B or a nested type, but those don't have to have been added by object B - and object B could have attached handlers from other classes. Basically there's nothing to say "who" subscribed to an event - only what the handler is.

1
  • O.k. thanks I figured as much and that I would be up against an architectural boundary. I thought there might have been some crazy "Reflection" based back door but no. Back to the drawing board then.
    – rism
    Apr 9, 2010 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.