Delegates - subscribing to events

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

ello everybody.

I have a class A that publishes an event, ands is using a delegate to allow
other objects to subscribe to it. After raising the event, class A is sending
some information wrapped up in the custom class deriving from EventArgs. When
the event fires, the class is then going through the list of subscribers
using the GetInvocationList() and is getting the response from the
subscribers.
So far so good.
The problem is when trying to subscribe to the event from a class B that has
no reference to class A.
It would be easy enough to add a reference to it and to just subscribe to
the event, however we do not want to make the server code to reference the
client classes.
Is there a way to do this differently? If not, that what about adding an
intermediary object that can receive and pass on the events raised in class A
on the client side, so that class B on the server needs only to reference one
object* But if there's a better way then I would really appreciate some
directions.

Thanks a lot.
 
Mirano said:
Is there a way to do this differently? If not, that what about adding an
intermediary object that can receive and pass on the events raised in class A
on the client side, so that class B on the server needs only to reference one
object* But if there's a better way then I would really appreciate some
directions.

This is essentially what is done in .NET remoting.

A "remotable" object is created on the client side, that subscibes to
the events the server raises. The client.exe references the remoting
object.

Check out the .NET sample code for chat applications.
 
Mirano said:
I have a class A that publishes an event, ands is using a delegate to allow
other objects to subscribe to it. After raising the event, class A is sending
some information wrapped up in the custom class deriving from EventArgs. When
the event fires, the class is then going through the list of subscribers
using the GetInvocationList() and is getting the response from the
subscribers.
So far so good.
The problem is when trying to subscribe to the event from a class B that has
no reference to class A.
It would be easy enough to add a reference to it and to just subscribe to
the event, however we do not want to make the server code to reference the
client classes.

I would suggest that you encapsulate the "I have an event Foo" in an
interface. The client class can implement the interface, but the server
classes wouldn't need to know that the client classes are involved - it
would just need to know about the interface itself.

You could then pass an instance of class A to class B *as a reference
to an instance of this interface* without making any assumptions about
what's actually implementing the interface.

Jon
 
Back
Top