Type.GetEvents Method
Gets the events that are declared or inherited by the current Type.
Overload List
Returns all the public events that are declared or inherited by the current Type.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Overridable Function GetEvents() As EventInfo()
[C#] public virtual EventInfo[] GetEvents();
[C++] public: virtual EventInfo* GetEvents() [];
[JScript] public function GetEvents() : EventInfo[];
When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public MustOverride Function GetEvents(BindingFlags) As EventInfo()
[C#] public abstract EventInfo[] GetEvents(BindingFlags);
[C++] public: virtual EventInfo* GetEvents(BindingFlags) [] = 0;
[JScript] public abstract function GetEvents(BindingFlags) : EventInfo[];
Example
[Visual Basic, C#, C++] The following example obtains an array of EventInfo objects that match the specified binding flags, gets all the events for a Button class, and displays the event names. To compile the Visual Basic example, use the following command line:
[Visual Basic, C#, C++] vbc type_getevents2.vb /r:System.Windows.Forms.dll /r:System.dll
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of GetEvents. For other examples that might be available, see the individual overload topics.
[Visual Basic] Imports System Imports System.Reflection Imports System.Security Imports System.Windows.Forms Imports Microsoft.VisualBasic Class EventsSample Public Shared Sub Main() Try ' Create a bitmask based on BindingFlags. Dim myBindingFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.Public Dim myTypeEvent As Type = GetType(System.Windows.Forms.Button) Dim myEventsBindingFlags As EventInfo() = myTypeEvent.GetEvents(myBindingFlags) Console.WriteLine(ControlChars.Cr + "The events on the Button class with the specified BindingFlags are:") Dim index As Integer For index = 0 To myEventsBindingFlags.Length - 1 Console.WriteLine(myEventsBindingFlags(index).ToString()) Next index Catch e As SecurityException Console.WriteLine("SecurityException:" + e.Message) Catch e As ArgumentNullException Console.WriteLine("ArgumentNullException: " + e.Message) Catch e As Exception Console.WriteLine("Exception: " + e.Message) End Try End Sub 'Main End Class 'EventsSample [C#] using System; using System.Reflection; using System.Security; class EventsSample { public static void Main() { try { // Create a bitmask based on BindingFlags. BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public; Type myTypeEvent = typeof(System.Windows.Forms.Button); EventInfo[] myEventsBindingFlags = myTypeEvent.GetEvents(myBindingFlags); Console.WriteLine("\nThe events on the Button class with the specified BindingFlags are:"); for (int index = 0; index < myEventsBindingFlags.Length; index++) { Console.WriteLine(myEventsBindingFlags[index].ToString()); } } catch(SecurityException e) { Console.WriteLine("SecurityException:" + e.Message); } catch(ArgumentNullException e) { Console.WriteLine("ArgumentNullException: " + e.Message); } catch(Exception e) { Console.WriteLine("Exception: " + 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; int main() { try { // Create a bitmask based on BindingFlags. BindingFlags myBindingFlags = static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public); Type* myTypeEvent = __typeof(System::Windows::Forms::Button); EventInfo* myEventsBindingFlags[] = myTypeEvent->GetEvents(myBindingFlags); Console::WriteLine(S"\nThe events on the Button class with the specified BindingFlags are:"); for (int index = 0; index < myEventsBindingFlags->Length; index++) { Console::WriteLine(myEventsBindingFlags[index]); } } catch (SecurityException* e) { Console::WriteLine(S"SecurityException: {0}", e->Message); } catch (ArgumentNullException* e) { Console::WriteLine(S"ArgumentNullException: {0}", e->Message); } catch (Exception* e) { Console::WriteLine(S"Exception: {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.