Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET中事件的GetInvocationList_.net_Vb.net_Events_Delegates - Fatal编程技术网

VB.NET中事件的GetInvocationList

VB.NET中事件的GetInvocationList,.net,vb.net,events,delegates,.net,Vb.net,Events,Delegates,我试图通过以下一个WCF应用程序示例(来自Sacha Barber)学习一些WCF原则 现在我想把下面的函数转换成VB.NET private void BroadcastMessage(ChatEventArgs e) { ChatEventHandler temp = ChatEvent; if (temp != null) { foreach (ChatEventHandler handler in temp.GetInvocationList(

我试图通过以下一个WCF应用程序示例(来自Sacha Barber)学习一些WCF原则

现在我想把下面的函数转换成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);
        }
    }
}
但是我有一些问题,因为编译器不接受下面的代码

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
上面说

公共共享事件聊天室事件(发件人) 作为对象,e作为ChatEventArgs)“是 事件,并且不能直接调用


说到这里,那么在VB.NET中是否有可能以其他方式将处理程序链接到某个事件?

您可能试图在声明
ChatEvent
事件的类的后代类中编写此代码。这是无法做到的,因为事件只能在声明它们的类中作为变量处理(包括调用它们)。这是因为
事件
关键字实际上向编译器指示它需要执行一些幕后转换

发生了什么

考虑这一宣言:

Public Event MyEvent as EventHandler
很简单,对吧?然而,这实际上是这样的(你只是看不到)

当您调用事件时:

RaiseEvent MyEvent(Me, EventArgs.Emtpy)
它实际上调用
RaiseEvent
块中的代码

编辑

如果VB.NET中的事件在任何地方都不能被视为变量(它们可以在C#中的声明类中被视为变量,这就是C#示例编译的原因),那么您必须自己显式实现该事件。有关如何执行此操作的更多信息,请参阅。长话短说,您需要某种方法来存储多个事件处理程序(或者使用单个事件处理程序和
GetInvocationList
,就像您现在尝试的那样)。在
AddHandler
RemoveHandler
代码块中,您将分别向事件处理程序列表中添加和删除事件处理程序

您可以使用以下内容:

Private myEventList as New List(Of EventHandler)

Public Custom Event MyEvent as EventHandler
    AddHandler(ByVal value as EventHandler)
        myEventList.Add(value)
    End AddHandler
    RemoveHandler(ByVal value as EventHandler)
        myEventList.Remove(value)
    End RemoveHandler
    RaiseEvent(ByVal sender as Object, ByVal e as EventArgs)
        For Each evt in myEventList
           evt.BeginInvoke(sender, e, New AsyncCallback(AddressOf EndAsync), Nothing)
        Next
    EndRaiseEvent
End Event
那么现在,如果你打电话

RaiseEvent MyEvent(Me, EventArgs.Emtpy)
它将以您期望的方式引发事件。

使用ChatEventEvent(或EventNameEvent)

它不会出现在intellisense中,但它的成员会

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


这仅在声明事件的类(或其子类)中可用。

谢谢您的回答,但我认为我的情况并非如此。我的类称为ChatService,它声明了共享事件ChatEvent和Sub BroadcastMessage,我试图从中获取ChatEvent的调用列表。在上面的第一个示例中,compilerGeneratedName始终是事件名称,后跟单词“Event”,因此,在这种情况下,ChatEventEvent。感谢您提供的信息,它们可能会有所帮助,为了努力,瑞克,是的!这让梅开了个玩笑。我不知道你能把这一切都用在一个活动上。看起来像是一个财产。但事实并非如此。请记住,您在类中使用的是未记录的成员,并且(除非我弄错了)此命名策略不保证在未来的编译器版本中保持不变。@Adam-是的,据我所知,绝对是未记录的,但我看不到他们更改它,除非他们有理由,或者至少提供一个替代方案。在任何情况下,这是目前唯一可以做到的方法,而不必像您的示例中那样独自处理所有事件。如果他们改变了,你的工作仍然有效,但考虑到它可能会改变,我宁愿保留这项工作。
RaiseEvent MyEvent(Me, EventArgs.Emtpy)