VB.NET中事件的GetInvocationList [英] GetInvocationList of an event in VB.NET

查看:140
本文介绍了VB.NET中事件的GetInvocationList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过一个WCF应用程序的例子(来自Sacha Barber)来学习一些WCF原理。



现在我想将以下函数转换成VB.NET

  private void BroadcastMessage(ChatEventArgs e)
{

ChatEventHandler temp = ChatEvent;

if(temp!= null)
{
foreach(temp.GetInvocationList()中的ChatEventHandler处理程序)
{
handler.BeginInvoke(this, e,新的AsyncCallback(EndAsync),null);
}
}
}

但我有一些问题,因为编译器不接受以下代码

  Private Sub BroadcastMessage(ByVal e As ChatEventArgs)

Dim处理程序作为EventHandler(ChatEventArgs)= ChatEvent

如果处理程序IsNot Nothing然后

对于每个处理程序作为EventHandler(ChatEventArgs)在Handlers.GetInvocationList()

handler.BeginInvoke(Me,e,New AsyncCallback(AddressOf EndAsync),Nothing)

下一个

如果

结束Sub

它说


公共共享事件ChatEvent(sender
As Object,e As ChatEventArgs)'是一个
事件,不能直接调用


到目前为止,是否可以在VB.NET中以某种其他方式获取与特定事件链接的处理程序?

解决方案

使用ChatEventEvent(或 EventName 事件)



将不会出现在智能感知中,但其成员将会。



VB.NET在幕后创建一个变量,以隐藏编码器的复杂性...



这只适用于声明事件(或其后代)的类


I am trying to learn some WCF principles by following an example of a WCF application (from Sacha Barber).

Now I would like to convert the following function into VB.NET

private void BroadcastMessage(ChatEventArgs e)
{

    ChatEventHandler temp = ChatEvent;

    if (temp != null)
    {
        foreach (ChatEventHandler handler in temp.GetInvocationList())
        {
            handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);
        }
    }
}

but I have some problems, because the following code is not accepted by the compiler

Private Sub BroadcastMessage(ByVal e As ChatEventArgs)

    Dim handlers As EventHandler(Of ChatEventArgs) = ChatEvent

    If handlers IsNot Nothing Then

        For Each handler As EventHandler(Of ChatEventArgs) In handlers.GetInvocationList()

            handler.BeginInvoke(Me, e, New AsyncCallback(AddressOf EndAsync), Nothing)

        Next

    End If

End Sub

it says

Public Shared Event ChatEvent(sender As Object, e As ChatEventArgs)' is an event, and cannot be called directly

Coming to the point, is it then possible in VB.NET get the handlers linked to a certain event in some other way?

解决方案

use ChatEventEvent (or EventNameEvent)

It won't show up in intellisense, but the members of it will.

VB.NET creates a variable behind the scenes, to hide the complexity from the coder...

This is only available in the class that declares the event (or perhaps its descendants)

这篇关于VB.NET中事件的GetInvocationList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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