Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to be notified when something subscribes to an event / delegate?

Is there any way of being notified when something subscribes to an event in my class, or do I need to wrap subscription/unsubsription in methods eg:

public class MyClass : ISomeInterface
{
     public event SomeEventHandler SomeEvent; //How do I know when something subscribes?

     private void OnSomeEventSubscription(SomeEventHandler handler)
     { 
          //do some work
     }

     private void OnSomeEventUnsubscription(SomeEventHandler handler)
     { 
          //do some work
     }
}

instead of

public class MyClass : ISomeInterface
{
     private SomeEventHandler _someEvent;

     public void SubscribeToSomeEvent(SomeEventHandler handler)
     {
          _someEvent += handler;

          //do some work
     }

     public void UnsubscribeFromSomeEvent(SomeEventHandler handler)
     {
          _someEvent -= handler;

          //do some work
     }    
}

The reason I ask is because the event is already exposed directly on a ISomeInterface but this particular implementation needs to know when stuff subscribes/unsubscribes.

like image 354
GazTheDestroyer Avatar asked Feb 06 '12 10:02

GazTheDestroyer


People also ask

What is the difference between event and delegate?

A delegate is declared outside a class whereas, an event is declared inside a class. To invoke a method using a delegate object, the method has to be referred to the delegate object. On the other hand, to invoke a method using an event object the method has to be referred to the event object.

Is an event handler a delegate?

The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.

What is an event handler method?

Event handlers In the event handler, you perform the actions that are required when the event is raised, such as collecting user input after the user clicks a button. To receive notifications when the event occurs, your event handler method must subscribe to the event.

Which operator would you use to have an event handler method subscribe to an event?

To subscribe to events programmatically Use the addition assignment operator ( += ) to attach an event handler to the event.


2 Answers

You can write custom accessors for your event:

private SomeEventHandler _someEvent;
public event SomeEventHandler SomeEvent
{
    add
    {
        _someEvent += value;
        Console.WriteLine("Someone subscribed to SomeEvent");
    }
    remove
    {
        _someEvent -= value;
        Console.WriteLine("Someone unsubscribed from SomeEvent");
    }
}
like image 126
Thomas Levesque Avatar answered Sep 19 '22 13:09

Thomas Levesque


Thomas has answered this already but thought I'd also add that you may need to lock any critical section in the add remove sections since event subscription is never thread safe, i.e. you have no idea who is going to connect to you or when. E.g.:

    private readonly object _objectLock = new object(); 
    private SomeEventHandler _someEvent; 

    public event SomeEventHandler SomeEvent
    {
        add
        {      
            lock(_objectLock)
            {
                _someEvent += value;
                // do critical processing here, e.g. increment count, etc could also use Interlocked class.
            } // End if
        } // End of class
        remove
        {
            lock(_objectLock)
            {
                 _someEvent -= value;
                // do critical processing here, e.g. increment count, etc could also use Interlocked class.
            } // End if
        } // End if
    } // End of event
like image 33
Jeb Avatar answered Sep 20 '22 13:09

Jeb