开发者

Log who subscribs to a event

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.


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.
        }
    }
}


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.


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.


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;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜