Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I use the explicit syntax for declaring an event, is it possible to get the number of subscribers inside the add {} and remove {} code blocks? For example:

C#
public event EventHandler MyEvent
{
    add
    {
        MyEvent += value;
        // Can I get a count of subscribers here?
    }
    remove
    {
        MyEvent -= value;
        // And also here?
    }
}


What I have tried:

I have found a way to do this in other parts of the code, using...

C#
MyEvent.GetInvocationList().Count


...but I can't figure out how or even if this syntax can be used inside the event declaration code.
Posted
Updated 14-Mar-17 0:33am
Comments
Richard MacCutchan 14-Mar-17 5:56am    
What happens when you try it?
Patrick Skelton 14-Mar-17 6:03am    
It won't compile. It says something like 'MyEvent can only appear on the left hand side of += or -='.
Bernhard Hiller 14-Mar-17 6:16am    
What about this.GetInvocationList()?
Patrick Skelton 14-Mar-17 6:19am    
The 'this' keyword appears to refer to the enclosing class, so I get a message saying that the class does not contain a method called GetInvocationList().

I'm guessing maybe it isn't possible because what I am trying to do is get a reference to the class instance that the C# compiler generates automatically in response to the 'event' keyword.
Tomas Takac 14-Mar-17 6:22am    
Why? What is your use case? I don't think you can do this from inside the add/remove accessors. If you have custom accessors the you can have a counter which you increment or decrement on add/remove. Or you can drop the custom accessors and get the count via GetInvocationList() anywhere inside your class. Depends on what you are trying to achieve.

How about this?
C#
private EventHandler _myEvent;
private int _myEventSubscribersCount;

public event EventHandler MyEvent
{
    add
    {
        _myEvent += value;
        _myEventSubscribersCount = _myEvent.GetInvocationList().Length;
    }

    remove
    {
        _myEvent -= value;
        _myEventSubscribersCount = _myEvent.GetInvocationList().Length;
    }
}
 
Share this answer
 
Comments
Patrick Skelton 14-Mar-17 13:49pm    
Excellent. Actually, since all I am doing is a bit of debug logging inside the 'add' and 'remove', I don't even need the count-variable. Which is nice because it means I don't need a single #if DEBUG, and my final release code is almost the same as if I had used the simpler property-like event declaration syntax.

Thank you very much.
[no name] 14-Mar-17 16:21pm    
You're welcome. Thanks for the reply.
Nice 'problem', I needed this construction to start and stop a timer driven option...
Happy coding, Peter
You can only do it from within the class that creates the event:
public class MyClass
    {
    public event EventHandler MyEvent;
    protected virtual void OnMyEvent(EventArgs e)
        {
        EventHandler eh = MyEvent;
        if (eh != null)
            {
            eh(this, e);
            }
        }
    public int CountEvents()
        {
        return MyEvent.GetInvocationList().Length;
        }
    }
...
private void MyButton_Click(object sender, EventArgs e)
    {
    MyClass mc = new MyClass();
    mc.MyEvent += mc_MyEvent;
    Console.WriteLine(mc.CountEvents());
    mc.MyEvent += mc_MyEvent;
    Console.WriteLine(mc.CountEvents());
 
Share this answer
 
Comments
Patrick Skelton 14-Mar-17 13:51pm    
I am accepting this as a solution, but the solution by Peter Vetger seems neater in my particular use-case.

Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900