在 GetInvocationList() 之后获得合适的 eventHandler [英] Getting suitable eventHandler after GetInvocationList()
问题描述
我有
public delegate void AdministrationEventHandler(object sender, AdministrationEventArgs e);
public static event AdministrationEventHandler MainAdministrationEventHandler;
它是我的 wcf 服务中的主要事件处理程序.我也有
It's main event handler in my wcf service. I have also
private void MyEventHandler(object sender, AdministrationEventArgs e)
{
//code
}
我添加新元素
AdministrationEventHandler userToAddEventHandler = new AdministrationEventHandler(MyEventHandler);
然后
MainAdministrationEventHandler += userToAddEventHandler;
在我的 Broadcast()
方法中,我向每个处理程序调用 MyEventHandler
.我想根据 AdministrationEventArgs e
参数调用 Broadcast()
.
In my Broadcast()
method I call MyEventHandler
to each handler.
I'd like to call Broadcast()
according to the AdministrationEventArgs e
argument.
我尝试制作类似于 MainAdministrationEventHandler
的列表并调用 Broadcast()
以获得此列表的适当元素,但它不起作用.
I tried to make something like list of MainAdministrationEventHandler
and call Broadcast()
for appropiate element of this list, but it doesn't work.
我通过 MainAdministrationEventHandler.GetInvocationList()
是否可以在GetInvocationList()
中获取e
参数后对其进行检查?
Is it possible to check e
argument after getting it in GetInvocationList()
?
或者我如何以其他方式调用 Broadcast()
到合适的处理程序?
Or how can I in other way call Broadcast()
to suitable handlers?
推荐答案
什么是合适的处理程序?因为根据您的代码,您只能将 AdministrationEventHandler
类型的处理程序分配给该事件.
What's a suitable handler? Because based on your code you can only assign handlers of type AdministrationEventHandler
to that event.
此外,从 GetInvocationList()
返回的列表仅包含要调用的处理程序.这次还没有调用它们,您需要调用它们并将 e
传递给它们.
Also, the list returned from GetInvocationList()
only contains the handlers to invoke. They haven't been invoked yet this time around, and you need to invoke them and pass e
to them.
应该这样做:
AdministrationEventArgs args = new AdministrationEventArgs();
Delegate[] dels = MainAdministrationEventHandler.GetInvocationList();
if (dels != null)
foreach (Delegate handler in dels)
handler.Invoke(this, args);
这篇关于在 GetInvocationList() 之后获得合适的 eventHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!