Passing events as arguments to functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Israel

    Passing events as arguments to functions

    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 GenericEventFor warder<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<My ArgsSomethingCh anged
    {
    Add
    {
    m_Forwarder.Add (value, new
    EventHandler<My Args>(m_Server. SomethingChange d));
    }
    Remove
    {
    m_Forwarder.Rem ove(value, new
    EventHandler<My Args>(m_Server. SomethingChange d));
    }
    }

    The forwarder class would maintain it’s own delegate chain via
    Delegate.Combin e() and Delegate.Remove () keeping track of the count of
    delegates.

  • Jon Skeet [C# MVP]

    #2
    Re: Passing events as arguments to functions

    Israel <israeldiperi@h otmail.comwrote :
    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.


    <snip>
    The forwarder class would maintain it=3Fs own delegate chain via
    Delegate.Combin e() 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.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Israel

      #3
      Re: Passing events as arguments to functions

      On Sep 17, 3:57 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
      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.

      Comment

      Working...