Type.GetEvents Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the events that are declared or inherited by the current Type.
Overloads
GetEvents() |
Returns all the public events that are declared or inherited by the current Type. |
GetEvents(BindingFlags) |
When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints. |
GetEvents()
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
Returns all the public events that are declared or inherited by the current Type.
public:
virtual cli::array <System::Reflection::EventInfo ^> ^ GetEvents();
public virtual System.Reflection.EventInfo[] GetEvents ();
abstract member GetEvents : unit -> System.Reflection.EventInfo[]
override this.GetEvents : unit -> System.Reflection.EventInfo[]
Public Overridable Function GetEvents () As EventInfo()
Returns
An array of EventInfo objects representing all the public events which are declared or inherited by the current Type.
-or-
An empty array of type EventInfo, if the current Type does not have public events.
Implements
Examples
The following example obtains an array of EventInfo objects, gets all the events for a Button
class, and displays the event names. To compile the Visual Basic example, use the following command line:
vbc type_getevents1.vb /r:System.Windows.Forms.dll /r:System.dll
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
int main()
{
try
{
Type^ myType = System::Windows::Forms::Button::typeid;
array<EventInfo^>^myEvents = myType->GetEvents();
Console::WriteLine( "The events on the Button class are: " );
for ( int index = 0; index < myEvents->Length; index++ )
{
Console::WriteLine( myEvents[ index ] );
}
}
catch ( SecurityException^ e )
{
Console::WriteLine( "SecurityException: {0}", e->Message );
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "ArgumentNullException: {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
using System;
using System.Reflection;
using System.Security;
class EventsSample
{
public static void Main()
{
try
{
// Creates 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);
}
}
}
open System
open System.Reflection
open System.Security
try
// Creates a bitmask based on BindingFlags.
let myBindingFlags = BindingFlags.Instance ||| BindingFlags.Public
let myTypeEvent = typeof<System.Windows.Forms.Button>
let myEventsBindingFlags = myTypeEvent.GetEvents myBindingFlags
printfn "\nThe events on the Button class with the specified BindingFlags are : "
for flag in myEventsBindingFlags do
printfn $"{flag}"
with
| :? SecurityException as e ->
printfn $"SecurityException: {e.Message}"
| :? ArgumentNullException as e ->
printfn $"ArgumentNullException: {e.Message}"
| e ->
printfn $"Exception : {e.Message}"
Imports System.Reflection
Imports System.Security
' Compile this sample using the following command line:
' vbc type_getevents.vb /r:"System.Windows.Forms.dll" /r:"System.dll"
Class EventsSample
Public Shared Sub Main()
Try
' Creates 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
End Class
Remarks
An event is considered public to reflection if it has at least one method or accessor that is public. Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or
) to get it.
In .NET 6 and earlier versions, the GetEvents method does not return events in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which events are returned, because that order varies. However, starting with .NET 7, the ordering is deterministic based upon the metadata ordering in the assembly.
This method can be overridden by a derived class.
The following table shows what members of a base class are returned by the Get
methods when reflecting on a type.
Member Type | Static | Non-Static |
---|---|---|
Constructor | No | No |
Field | No | Yes. A field is always hide-by-name-and-signature. |
Event | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
Method | No | Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
Nested Type | No | No |
Property | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.
For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
Custom attributes are not part of the common type system.
If the current Type represents a constructed generic type, this method returns the EventInfo objects with the type parameters replaced by the appropriate type arguments.
If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the events of the class constraint.
See also
Applies to
GetEvents(BindingFlags)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints.
public:
abstract cli::array <System::Reflection::EventInfo ^> ^ GetEvents(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo[] GetEvents (System.Reflection.BindingFlags bindingAttr);
abstract member GetEvents : System.Reflection.BindingFlags -> System.Reflection.EventInfo[]
Public MustOverride Function GetEvents (bindingAttr As BindingFlags) As EventInfo()
Parameters
- bindingAttr
- BindingFlags
A bitwise combination of the enumeration values that specify how the search is conducted.
-or-
Default to return an empty array.
Returns
An array of EventInfo objects representing all events that are declared or inherited by the current Type that match the specified binding constraints.
-or-
An empty array of type EventInfo, if the current Type does not have events, or if none of the events match the binding constraints.
Implements
Examples
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:
vbc type_getevents2.vb /r:System.Windows.Forms.dll /r:System.dll
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.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 = System::Windows::Forms::Button::typeid;
array<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 ] );
}
}
catch ( SecurityException^ e )
{
Console::WriteLine( "SecurityException: {0}", e->Message );
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "ArgumentNullException: {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
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);
}
}
}
open System
open System.Reflection
open System.Security
try
// Creates a bitmask based on BindingFlags.
let myBindingFlags = BindingFlags.Instance ||| BindingFlags.Public
let myTypeEvent = typeof<System.Windows.Forms.Button>
let myEventsBindingFlags = myTypeEvent.GetEvents myBindingFlags
printfn "\nThe events on the Button class with the specified BindingFlags are : "
for flag in myEventsBindingFlags do
printfn $"{flag}"
with
| :? SecurityException as e ->
printfn $"SecurityException: {e.Message}"
| :? ArgumentNullException as e ->
printfn $"ArgumentNullException: {e.Message}"
| e ->
printfn $"Exception : {e.Message}"
Imports System.Reflection
Imports System.Security
Imports System.Windows.Forms
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
End Class
Remarks
In .NET 6 and earlier versions, the GetEvents method does not return events in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which events are returned, because that order varies. However, starting with .NET 7, the ordering is deterministic based upon the metadata ordering in the assembly.
The following BindingFlags filter flags can be used to define which events to include in the search:
You must specify either
BindingFlags.Instance
orBindingFlags.Static
in order to get a return.Specify
BindingFlags.Public
to include public events in the search.Specify
BindingFlags.NonPublic
to include non-public events (that is, private, internal, and protected events) in the search. Only protected and internal events on base classes are returned; private events on base classes are not returned.Specify
BindingFlags.FlattenHierarchy
to includepublic
andprotected
static members up the hierarchy;private
static members in inherited classes are not included.
The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.DeclaredOnly
to search only the events declared on the Type, not events that were simply inherited.
See System.Reflection.BindingFlags for more information.
An event is considered public to reflection if it has at least one method or accessor that is public. Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or
) to get it.
If the current Type represents a constructed generic type, this method returns the EventInfo objects with the type parameters replaced by the appropriate type arguments.
If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the events of the class constraint.