简体   繁体   English

如何在C#中获取显式事件的调用列表

[英]How to get invocation list of an explicit event in C#

I can define an event with the following: 我可以使用以下内容定义事件:

public event msg_callback event_PingMessage; 

and get the invocation list of an event with the following: 并使用以下命令获取事件的调用列表:

MulticastDelegate event_delegate = (MulticastDelegate)this.GetType().GetField(event_name,
                                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField).GetValue(this);

foreach (var handler in event_delegate.GetInvocationList())
{
    // use handler() here 
}

But, if I define my event_PingMessage as an explicit event, like: 但是,如果我将event_PingMessage定义为显式事件,例如:

    private msg_callback explicitEvent;
    public event msg_callback event_PingMessage
    {
        add
        {
            explicitEvent += value;
            int i = 0; 

        }
        remove
        {
            explicitEvent -= value;
        }
    }

the MulticastDelegate event_delegate = ... line throws an exception: MulticastDelegate event_delegate = ...行引发异常:

Object reference not set to an instance of an object.

How can I .GetInvocationList() for my explicit events? 如何为我的显式事件.GetInvocationList()

You can just use explicitEvent.GetInvocationList() . 您可以只使用explicitEvent.GetInvocationList() explicitEvent is the equivalent of the field returned by your GetField(...) call for a field-like event. explicitEvent等效于您的GetField(...)调用返回的类似字段的事件的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#事件调用列表过滤 - C# Event invocation list filtering 如何使用C#明确方法调用顺序? - How can I make method invocation order explicit with C#? 如何在运行时(C#+ Unity)将方法添加到ANY事件的调用列表中 - How do I add a method to ANY event's invocation list at runtime (C# + Unity) 如何使用 C# 获取事件查看器中所有提供程序的列表? - How to get List of all Providers in Event Viewer using C#? 在C#中,如何将默认get与显式集合混合? - In C#, how do you mix a default get with an explicit set? 如何获取任意事件的调用列表 - How to obtain the invocation list of any event 如何在 C# 中获取所有 Windows 事件日志(事件查看器日志)及其层次结构和友好名称的列表 - How to get a list of all Windows Event Logs (Event Viewer Logs) with their hierarchy and friendly names in C# 如何编写扩展方法,以使多播C#委托中的调用列表成员顺序运行? - How to write extension method which will make invocation list members in multicast C# delegate run sequentially? 如何获取委托调用的结果列表? - How to get results list of delegate's invocation? VB到C#-事件处理程序的自动命名与显式接线 - VB to C# - auto naming of event handlers vs explicit wiring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM