EventInfo Class
Discovers the attributes of an event and provides access to event metadata.
Assembly: mscorlib (in mscorlib.dll)
'Declaration <SerializableAttribute> _ <ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.None)> _ <PermissionSetAttribute(SecurityAction.InheritanceDemand, Name := "FullTrust")> _ Public MustInherit Class EventInfo _ Inherits MemberInfo _ Implements _EventInfo 'Usage Dim instance As EventInfo
Use the EventInfo class to inspect events and to hook up event handlers, as shown in the example code for the AddEventHandler method.
Note: |
|---|
EventInfo is not intended to be used to raise events. An object raises events as dictated by its internal state. |
Events are used with delegates. An event listener instantiates an event-handler delegate that is invoked whenever the event is raised by an event source. In order to connect to the event source, the event listener adds this delegate to the invocation list on the source. When the event is raised, the invoke method of the event-handler delegate is called. Both multicast and single-cast event notifications are supported. The Add and Remove methods, as well as the event-handler delegate class associated with an event, must be marked in the metadata.
Delegates are object-oriented function pointers. In C or C++, a function pointer is a reference to a method. In contrast to the C or C++ function pointer, a delegate contains two references: a reference to a method and a reference to an object that supports the method. Delegates can invoke a method without knowing the class type that declares or inherits the method. Delegates need only know the return type and parameter list of the method.
The event model works equally well for single-cast and multicast delegates. When the delegate's invoke method is called, only a single object will have a method called on it. A multicast modifier can be applied to a delegate declaration, which allows multiple methods to be called when the invoke method of the delegate is called.
Calling ICustomAttributeProvider.GetCustomAttributes on EventInfo when the inherit parameter of GetCustomAttributes is true does not walk the type hierarchy. Use System.Attribute to inherit custom attributes.
Notes to Inheritors:When you inherit from EventInfo, you must override the following members: GetAddMethod, GetRemoveMethod, and GetRaiseMethod.
The following code gets an EventInfo object for the Click event of the Button class.
Imports System Imports System.Reflection Imports System.Security Imports Microsoft.VisualBasic ' Compile this sample using the following command line: ' vbc type_getevent.vb /r:"System.Windows.Forms.dll" /r:"System.dll" Class MyEventExample Public Shared Sub Main() Try ' Creates a bitmask comprising BindingFlags. Dim myBindingFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.Public _ Or BindingFlags.NonPublic Dim myTypeBindingFlags As Type = GetType(System.Windows.Forms.Button) Dim myEventBindingFlags As EventInfo = myTypeBindingFlags.GetEvent("Click", myBindingFlags) If myEventBindingFlags IsNot Nothing Then Console.WriteLine("Looking for the Click event in the Button class with the specified BindingFlags.") Console.WriteLine(myEventBindingFlags.ToString()) Else Console.WriteLine("The Click event is not available with the Button class.") End If Catch e As SecurityException Console.WriteLine("An exception occurred.") Console.WriteLine("Message :" + e.Message) Catch e As ArgumentNullException Console.WriteLine("An exception occurred.") Console.WriteLine("Message :" + e.Message) Catch e As Exception Console.WriteLine("The following exception was raised : {0}", e.Message) End Try End Sub 'Main End Class 'MyEventExample
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: