加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

GetInvocationList of an event in VB.NET

发布时间:2020-12-17 07:48:38 所属栏目:百科 来源:网络整理
导读:?? Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free,no registration required. GetInvocationList of an event in VB.NET up vote 6 down vote favorite 1 I am trying to learn some WCF prin
??
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free,no registration required.

GetInvocationList of an event in VB.NET

up vote 6 down vote favorite
1

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

share | improve this question

2 Answers 2

active oldest votes
up vote 3 down vote accepted

use ChatEventEvent (or *EventName*Event)

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)

share | improve this answer
Thanks a lot,this was the solution for me. Joy and pain of Intellisense :D – Drake Feb 1 '10 at 9:30
Just bear in mind that you're using an undocumented member on your class,and (unless I'm mistaken) this naming policy is not guaranteed to stay the same in future compiler versions. – Adam Robinson Feb 1 '10 at 13:58
@Adam - Yeah,definitely undocumented,as far as I've seen,but I can't see them changing it unless they have some reason to,or at least provide an alternative. In any case,it is the only way you can do it currently,without handling all of the event stuff on your own,as in your example. Yours will still work if they change it,but I would rather save that work given the likelihood that it will change. – Rick Mogstad Feb 1 '10 at 19:25
I am trying to achieve your answer on this post: geekswithblogs.net/NewThingsILearned/archive/2008/01/16/… using Dim handler as NotifyCollectionChangedEventArgs = CollectionChangedEvent but I get a compiler error. – Shimmy Feb 1 '10 at 20:49
up vote 3 down vote

You're likely trying to write this code in a class that is a descendant of the class that declares the ChatEvent event. This can't be done,as events can only be treated as variables (including invoking them) in the class that declares them. This is because the event keyword actually indicates to the compiler that it needs to perform some behind-the-scenes transformations.

What Happens

Consider this declaration:

Public Event MyEvent as EventHandler

Simple enough,right? What this actually does,however,is this (you just don't see it)

Private compilerGeneratedName as EventHandler

Public Event MyEvent as EventHandler
    AddHandler(ByVal value as EventHandler)
        compilerGeneratedName += value
    End AddHandler
    RemoveHandler(ByVal value as EventHandler)
        compilerGeneratedName -= value
    End RemoveHandler
    RaiseEvent(ByVal sender as Object,ByVal e as EventArgs)
        compilerGeneratedName.Invoke(sender,e)
    End RaiseEvent
End Event

And when you invoke the event:

RaiseEvent MyEvent(Me,EventArgs.Emtpy)

It actually calls the code in the RaiseEvent block.

Edit

If events in VB.NET can't be treated as variables anywhere (they can be treated as variables in the declaring class in C#,which is why your C# example compiles),then you'll have to explicitly implement the event yourself. See the MSDN page on the Event statement for more information on how to do that. To make a long story short,you're going to want some way to store multiple event handlers (or use a single event handler along with GetInvocationList,as you're trying to do now). In your AddHandler and RemoveHandler code blocks you'll add to and remove from the list of event handlers (respectively).

You could use something like this:

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,Nothing)
        Next
    EndRaiseEvent
End Event

So now,if you call

RaiseEvent MyEvent(Me,EventArgs.Emtpy)

It will raise the event in the fashion you're expecting.

share | improve this answer
Thank you for the answer,but I don't think is my case. My class is called ChatService and it declares both the Shared Event ChatEvent and the Sub BroadcastMessage,where I am trying to get the InvocationList of ChatEvent. – Drake Jan 29 '10 at 13:56
@marco: See if my edits are more helpful – Adam Robinson Jan 29 '10 at 14:14
1
in the above first example,compilerGeneratedName is always the event name followed by the word "Event",so,ChatEventEvent,in this case. – Rick Mogstad Jan 29 '10 at 22:49
thanks for the info,they could be helpful,and for the effort @Rick Yeah! That made the trick for me – Drake Feb 1 '10 at 9:29

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读