EventInfo Class
Discovers the attributes of an event and provides access to event metadata.
For a list of all members of this type, see EventInfo Members.
System.Object
System.Reflection.MemberInfo
System.Reflection.EventInfo
[Visual Basic] <ClassInterface(ClassInterfaceType.AutoDual)> MustInherit Public Class EventInfo Inherits MemberInfo [C#] [ClassInterface(ClassInterfaceType.AutoDual)] public abstract class EventInfo : MemberInfo [C++] [ClassInterface(ClassInterfaceType::AutoDual)] public __gc __abstract class EventInfo : public MemberInfo [JScript] public ClassInterface(ClassInterfaceType.AutoDual) abstract class EventInfo extends MemberInfo
Thread Safety
This type is safe for multithreaded operations.
Remarks
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.
Example
[Visual Basic] 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 Not (myEventBindingFlags Is 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 [C#] using System; using System.Reflection; using System.Security; class MyEventExample { public static void Main() { try { // Creates a bitmask based on BindingFlags. BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; Type myTypeBindingFlags = typeof(System.Windows.Forms.Button); EventInfo myEventBindingFlags = myTypeBindingFlags.GetEvent("Click", myBindingFlags); if(myEventBindingFlags != null) { 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."); } catch(SecurityException e) { Console.WriteLine("An exception occurred."); Console.WriteLine("Message :"+e.Message); } catch(ArgumentNullException e) { Console.WriteLine("An exception occurred."); Console.WriteLine("Message :"+e.Message); } catch(Exception e) { Console.WriteLine("The following exception was raised : {0}",e.Message); } } } [C++] #using <mscorlib.dll> #using <System.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Reflection; using namespace System::Security; using namespace System::Windows::Forms; int main() { try { // Creates a bitmask based on BindingFlags. BindingFlags myBindingFlags = static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public | BindingFlags::NonPublic); Type* myTypeBindingFlags = __typeof(System::Windows::Forms::Button); EventInfo* myEventBindingFlags = myTypeBindingFlags->GetEvent(S"Click", myBindingFlags); if (myEventBindingFlags != 0) { Console::WriteLine(S"Looking for the Click event in the Button class with the specified BindingFlags."); Console::WriteLine(myEventBindingFlags); } else Console::WriteLine(S"The Click event is not available with the Button class."); } catch (SecurityException* e) { Console::WriteLine(S"An exception occurred."); Console::WriteLine(S"Message : {0}", e->Message); } catch (ArgumentNullException* e) { Console::WriteLine(S"An exception occurred."); Console::WriteLine(S"Message : {0}", e->Message); } catch (Exception* e) { Console::WriteLine(S"The following exception was raised : {0}", e->Message); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Reflection
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: Mscorlib (in Mscorlib.dll)