It seems like there should be a way to pass events around like
delegates but on the other hand they’re sort of special delegates
because the only one allowed to invoke them is the owner of the event.
What I want to do is create a generic event forwarder that forwards
events from a server to a client. It will only subscribe to the
server events once and only if there’s at least one client
subscriber. Once the last client subscriber removes its event handler
then the forwarder will remove its event handler from the server.
To use this class it would look something like this:
m_Forwarder = new GenericEventForwarder<MyArgs>((object)this);
// pass this pointer so forwarder can use as sender of events so
clients think that we're the sender
public event EventHandler<MyArgsSomethingChanged
{
Add
{
m_Forwarder.Add(value, new
EventHandler<MyArgs>(m_Server.SomethingChanged));
}
Remove
{
m_Forwarder.Remove(value, new
EventHandler<MyArgs>(m_Server.SomethingChanged));
}
}
The forwarder class would maintain it’s own delegate chain via
Delegate.Combine() and Delegate.Remove() keeping track of the count of
delegates.