Delegates - subscribing to events

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.
 
J

John A. Bailo

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.
 
S

skeet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top