如何获取事件的用户? [英] How do I get the subscribers of an event?

查看:105
本文介绍了如何获取事件的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个事件的用户复制到另一个事件。我能得到一个事件的用户(如MyEvent [0]返回一个代表)?

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)?

如果无法做到这一点我会使用add访问向与会代表添加到列表中。请问这是最好的解决办法?

If this is not possible I would use the add accessor to add the delegates to a list. Would that be the best solution?

推荐答案

C#事件/委托是多播,所以委托是的本身的列表。从类中,得到个人的来电,你可以使用:

C# events/delegates are multicast, so the delegate is itself a list. From within the class, to get individual callers, you can use:

if(field != null) { // or the event-name for field-like events
    // or your own event-type in place of EventHandler
    foreach(EventHandler subscriber in field.GetInvocationList())
    {
        // etc
    }
}

不过,分配全部一次,只需使用+ =或直接分配:

However, to assign all at once, just use += or direct assignment:

SomeType other = ...
other.SomeEvent += localEvent;

这篇关于如何获取事件的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆