Passing events as arguments to functions

  • Thread starter Thread starter Israel
  • Start date Start date
I

Israel

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<MyArgs> SomethingChanged
{
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.
 
Israel said:
It seems like there should be a way to pass events around like
delegates but on the other hand they=3Fre sort of special delegates
because the only one allowed to invoke them is the owner of the event.

They're not delegates at all. They're a pair of methods - subscribe and
unsubscribe. Talking about passing them around is like talking about
passing a property around (rather than the *value* of a property). You
can of course pass a PropertyInfo (or an EventInfo) if necessary.
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=3Fs 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.

That sounds feasible. In that case, the EventInfo would actually give
you everything you need - admittedly you'd be calling the
subscribe/unsubscribe bits by reflection (unless you created a delegate
to do that - very confusing!) but it would do the trick.


The forwarder class would maintain it=3Fs own delegate chain via
Delegate.Combine() and Delegate.Remove() keeping track of the count of
delegates.

Just use += and -= as shortcuts or Combine and Remove - and when it
returns null, that means the last subscriber has been removed. No need
to do explicit counting.
 
That sounds feasible. In that case, the EventInfo would actually give
you everything you need - admittedly you'd be calling the
subscribe/unsubscribe bits by reflection (unless you created a delegate
to do that - very confusing!) but it would do the trick.

I guess I was originally thinking I wanted to avoid using reflection
due to the volume of events but since it would only be used for
subscribe / unsubscribe, and that won't happen that often, maybe it
wouldn't be such a bad solution. I always feel dirty using reflection
kind of like using IDispatch; I need to take a shower after using
either in code.
 
Back
Top