2

It it possible to retrive who is subscribing to a event in C#?

example

class MyClass
{
  public string Name { get; set; } 
}

class Syncronizer
{
  public delegate void SynchronizatonEventHandler(MyClass myClass);
  public event SynchronizatonEventHandler OnSyncFinished;
}

If i have something like that is it possible for me to see/use the myClass.Name string and use it for logging when the event is subscribed to?

What i want to accomplish is that i want to log every subscribe and unsubscribe from my Syncronizer class.

4 Answers 4

5

You can do the following:

class MyClass
{
    public string Name { get; set; }
}

class Syncronizer
{
    public delegate void SynchronizatonEventHandler(MyClass myClass);
    internal event SynchronizatonEventHandler _onSyncFinished;

    public event SynchronizatonEventHandler OnSyncFinished
    {
        add
        {
            // Perform some code before the subscription.
            // Add the event.
            _onSyncFinished += value;
            // Perform some code after the subscription;
        }
        remove
        {
            // Perform some code before the subscription.
            // Remove the event.
            _onSyncFinished -= value;
            // Peroform some code after the subscription.
        }
    }
}
2
  • 1
    This code would cause a StackOverflowException... You need to create a field of type EventHandler in addition to the event, and manipulate that field in the event accessors Commented Jul 19, 2011 at 14:03
  • i thought this was clear. i changed the answer with a complete solution.
    – dknaack
    Commented Jul 19, 2011 at 14:05
3

Here's a working example:

class Syncronizer
{
    public delegate void SynchronizatonEventHandler(MyClass myClass);
    private event SynchronizatonEventHandler onSyncFinished;
    public event SynchronizatonEventHandler OnSyncFinished
    {
        add
        {
            var method = new StackTrace().GetFrame(1).GetMethod();
            Console.WriteLine("{0}.{1} subscribing", method.ReflectedType.Name, method.Name);
            onSyncFinished += value;
        }
        remove
        {
            var method = new StackTrace().GetFrame(1).GetMethod();
            Console.WriteLine("{0}.{1} unsubscribing", method.ReflectedType.Name, method.Name);
            onSyncFinished -= value;
        }
    }
}

Note that you can not log myClass.Name, since that doesn't exist in the add and remove procedures. I have it logging (to Console.WriteLine) the class and method that subscribed to the event, which is, I think, what you were after.

1

You need to create an explicit event with your own accessors:

public event SynchronizatonEventHandler OnSyncFinished {
    add { ... }
    remove { ... }
}

add and remove take a value parameter containing the delegate instance being removed from or added to the event.
For logging purposes, you can get the Method and Target properties of the instance.

0

Sth. like this should solve your issue:

private event SynchronizatonEventHandler m_OnSyncFinished;

public event SynchronizatonEventHandler OnSyncFinished
{
  add
  {
    // Custom code could be added here...
    m_OnSyncFinished += value;
  }
  remove
  {
    // Custom code could be added here...
    m_OnSyncFinished -= value;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.