How do I get the subscribers of an event?

前端 未结 4 2078
一个人的身影
一个人的身影 2020-11-29 08:17

I need to copy the subscribers of one event to another event. Can I get the subscribers of an event (like MyEvent[0] returning a delegate)?

If this is not possible I

4条回答
  •  孤独总比滥情好
    2020-11-29 08:38

    In case you need to examine subscribers of an external class' event:

    EventHandler e = typeof(ExternalClass)
        .GetField(nameof(ExternalClass.Event), BindingFlags.Instance | BindingFlags.NonPublic)
        .GetValue(instanceOfExternalClass) as EventHandler;
    if (e != null)
    {
        Delegate[] subscribers = e.GetInvocationList();
    }
    

提交回复
热议问题