GetInvocationList of an event in VB.NET
2015-01-27 13:32
585 查看
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDM3LCJhdCI6NCwiYnQiOjAsImNtIjoyMDE1NDksImNoIjoxMTc4LCJjciI6Njk4OTU4LCJkaSI6IjZkZTU2MWJhNmMxNjRlNmQ5ZjBjYWRlYjYwZWJlNGRlIiwiZG0iOjEsImZjIjo3MjU1ODAsImZsIjo0NDM4NzQsImlwIjoiNjAuMTIuMTg1Ljk4Iiwia3ciOiIubmV0LHZiLm5ldCxldmVudHMsZGVsZWdhdGVzIiwibWsiOiIubmV0IiwibnciOjIyLCJwYyI6MywicHIiOjU3OTIsInJ0IjoyLCJyZiI6Imh0dHA6Ly9jbi5iaW5nLmNvbS9zZWFyY2g_cT1HZXRJbnZvY2F0aW9uTGlzdCtWQiZxcz1uJmZvcm09UUJMSCZwYz1FVVBQXyZwcT1nZXRpbnZvY2F0aW9ubGlzdCt2YiZzYz0xLTE4JnNwPS0xJnNrPSZjdmlkPWIwMzJkZjU1NTM2MTRmZDZiYzNkMmQzY2QyMTIxODBjIiwic3QiOjgyNzcsInVrIjoidWUxLWE4ODFjYzUwMjFhYTQzMTI4MDcwZTQ1MDU3ZmQyZWQxIiwiem4iOjQzLCJ0cyI6MTQyMjMyMjUzMDQ0NiwiYmYiOnRydWV9&s=nd3N-qNsfx-0JlJXrfxvzObCutM)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDM3LCJhdCI6NCwiYnQiOjAsImNtIjoyMDE1NDksImNoIjoxMTc4LCJjciI6Njk4OTYxLCJkaSI6ImEzZDgwNTE5ZDFkNjQ3YWM4ZDhkNGYxNDc0ZjRjMzM2IiwiZG0iOjEsImZjIjo3MjU1ODMsImZsIjo0NDM4NzQsImlwIjoiNjAuMTIuMTg1Ljk4Iiwia3ciOiIubmV0LHZiLm5ldCxldmVudHMsZGVsZWdhdGVzIiwibWsiOiIubmV0IiwibnciOjIyLCJwYyI6MywicHIiOjU3OTIsInJ0IjoyLCJyZiI6Imh0dHA6Ly9jbi5iaW5nLmNvbS9zZWFyY2g_cT1HZXRJbnZvY2F0aW9uTGlzdCtWQiZxcz1uJmZvcm09UUJMSCZwYz1FVVBQXyZwcT1nZXRpbnZvY2F0aW9ubGlzdCt2YiZzYz0xLTE4JnNwPS0xJnNrPSZjdmlkPWIwMzJkZjU1NTM2MTRmZDZiYzNkMmQzY2QyMTIxODBjIiwic3QiOjgyNzcsInVrIjoidWUxLWE4ODFjYzUwMjFhYTQzMTI4MDcwZTQ1MDU3ZmQyZWQxIiwiem4iOjQ0LCJ0cyI6MTQyMjMyMjUzMDQ0OCwiYmYiOnRydWV9&s=8QV_JZKgrD-AFUuiIIYNymdZE0c)
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
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDM3LCJhdCI6NCwiYnQiOjAsImNtIjoyMDE1NDksImNoIjoxMTc4LCJjciI6Njk4OTU4LCJkaSI6IjZkZTU2MWJhNmMxNjRlNmQ5ZjBjYWRlYjYwZWJlNGRlIiwiZG0iOjEsImZjIjo3MjU1ODAsImZsIjo0NDM4NzQsImlwIjoiNjAuMTIuMTg1Ljk4Iiwia3ciOiIubmV0LHZiLm5ldCxldmVudHMsZGVsZWdhdGVzIiwibWsiOiIubmV0IiwibnciOjIyLCJwYyI6MywicHIiOjU3OTIsInJ0IjoyLCJyZiI6Imh0dHA6Ly9jbi5iaW5nLmNvbS9zZWFyY2g_cT1HZXRJbnZvY2F0aW9uTGlzdCtWQiZxcz1uJmZvcm09UUJMSCZwYz1FVVBQXyZwcT1nZXRpbnZvY2F0aW9ubGlzdCt2YiZzYz0xLTE4JnNwPS0xJnNrPSZjdmlkPWIwMzJkZjU1NTM2MTRmZDZiYzNkMmQzY2QyMTIxODBjIiwic3QiOjgyNzcsInVrIjoidWUxLWE4ODFjYzUwMjFhYTQzMTI4MDcwZTQ1MDU3ZmQyZWQxIiwiem4iOjQzLCJ0cyI6MTQyMjMyMjUzMDQ0NiwiYmYiOnRydWV9&s=nd3N-qNsfx-0JlJXrfxvzObCutM)
up vote6down votefavorite 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, 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? .net vb.net events delegates
| ||
2 Answers 2
activeoldestvotesup vote3down voteaccepted | 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)
| ||||||||||||||||
|
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDM3LCJhdCI6NCwiYnQiOjAsImNtIjoyMDE1NDksImNoIjoxMTc4LCJjciI6Njk4OTYxLCJkaSI6ImEzZDgwNTE5ZDFkNjQ3YWM4ZDhkNGYxNDc0ZjRjMzM2IiwiZG0iOjEsImZjIjo3MjU1ODMsImZsIjo0NDM4NzQsImlwIjoiNjAuMTIuMTg1Ljk4Iiwia3ciOiIubmV0LHZiLm5ldCxldmVudHMsZGVsZWdhdGVzIiwibWsiOiIubmV0IiwibnciOjIyLCJwYyI6MywicHIiOjU3OTIsInJ0IjoyLCJyZiI6Imh0dHA6Ly9jbi5iaW5nLmNvbS9zZWFyY2g_cT1HZXRJbnZvY2F0aW9uTGlzdCtWQiZxcz1uJmZvcm09UUJMSCZwYz1FVVBQXyZwcT1nZXRpbnZvY2F0aW9ubGlzdCt2YiZzYz0xLTE4JnNwPS0xJnNrPSZjdmlkPWIwMzJkZjU1NTM2MTRmZDZiYzNkMmQzY2QyMTIxODBjIiwic3QiOjgyNzcsInVrIjoidWUxLWE4ODFjYzUwMjFhYTQzMTI4MDcwZTQ1MDU3ZmQyZWQxIiwiem4iOjQ0LCJ0cyI6MTQyMjMyMjUzMDQ0OCwiYmYiOnRydWV9&s=8QV_JZKgrD-AFUuiIIYNymdZE0c)
up vote3down vote | You're likely trying to write this code in a class that is a descendant of the class that declares theChatEventevent. 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 eventkeyword 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 And when you invoke the event: RaiseEvent MyEvent(Me, EventArgs.Emtpy) It actually calls the code in the RaiseEventblock. 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 AddHandlerand RemoveHandlercode 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, e, New AsyncCallback(AddressOf EndAsync), 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.
| ||||||||||||||||
|
相关文章推荐
- 获取本机已安装的软件列表C#/VB.NET(Get the list of installed software in local pc)
- Matt Meleski's .Net Blog - The ABC's of .NET : Constructing BizTalk 2004 XML Messages (In an Orchestration) - Choices
- Check/Uncheck all Items in an ASP.NET CheckBox List using jQuery
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- An overview of authentication security features in ASP. NET
- uiview 某点返回 色值 How to get the color of a pixel in an UIView?
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- Inversion of Control – An Introduction with Examples in .NET
- List of bugs that are fixed in the .NET Framework 1.1 Service Pack 1 (SP1)
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- An error message detailing the cause of this specific request failure can be found in the application event log of the
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- To get the tree list of the objects on which an object depends
- An example code in .net of AWS EC2
- An Overview Of The New Services, Controls, And Features In ASP.NET 2.0
- Foundations of WF: an Introduction to Windows Workflow Foundation (Expert's Voice in .Net)
- How Would You Get the Count of an Array in C++?
- Zhuanzai: change Asp.net Themes dynamicly (Setting An ASP.NET Theme in the PreInit Event Handler)
- 基于POSIX在Linux下C语言实现读取整个目录文件[Get a List of Files in a Directory with C]
- (.NET) IntelliSense difference of Extension Method name in Statement Completion for VB and C#.