Type.GetEvents Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Returns all the public events that are declared or inherited by the current Type.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Overridable Function GetEvents As EventInfo()
public virtual EventInfo[] GetEvents()
Return Value
Type: array<System.Reflection.EventInfo[]
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.
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.
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.
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. |
Notes:
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.
Examples
The following example obtains an array of EventInfo objects, gets all the events for a class, and displays the event names.
Note: |
---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
Imports System.Reflection
Public Class Example
Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
For Each evi As EventInfo In outputBlock.GetType().GetEvents()
outputBlock.Text &= evi.ToString() & vbLf
Next evi
End Sub
End Class
' This example produces output similar to the following:
'
'System.Windows.RoutedEventHandler Loaded
'System.Windows.SizeChangedEventHandler SizeChanged
'System.Windows.EventHandler LayoutUpdated
'System.EventHandler`1[System.Windows.Controls.ValidationErrorEventArgs] BindingValidationError
'System.Windows.Input.MouseEventHandler MouseMove
'System.Windows.Input.MouseEventHandler MouseEnter
'System.Windows.Input.MouseEventHandler MouseLeave
'System.Windows.Input.MouseButtonEventHandler MouseLeftButtonDown
'System.Windows.Input.MouseButtonEventHandler MouseLeftButtonUp
'System.Windows.Input.KeyEventHandler KeyUp
'System.Windows.Input.KeyEventHandler KeyDown
'System.Windows.Input.Mouse EventHandler
'System.Windows.RoutedEventHandler GotFocus
'System.Windows.RoutedEventHandler LostFocus
'System.Windows.Input.MouseEventHandler LostMouseCapture
using System;
using System.Reflection;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
foreach( EventInfo evi in outputBlock.GetType().GetEvents() )
{
outputBlock.Text += evi.ToString() + "\n";
}
}
}
/* This example produces output similar to the following:
System.Windows.RoutedEventHandler Loaded
System.Windows.SizeChangedEventHandler SizeChanged
System.Windows.EventHandler LayoutUpdated
System.EventHandler`1[System.Windows.Controls.ValidationErrorEventArgs] BindingValidationError
System.Windows.Input.MouseEventHandler MouseMove
System.Windows.Input.MouseEventHandler MouseEnter
System.Windows.Input.MouseEventHandler MouseLeave
System.Windows.Input.MouseButtonEventHandler MouseLeftButtonDown
System.Windows.Input.MouseButtonEventHandler MouseLeftButtonUp
System.Windows.Input.KeyEventHandler KeyUp
System.Windows.Input.KeyEventHandler KeyDown
System.Windows.Input.Mouse EventHandler
System.Windows.RoutedEventHandler GotFocus
System.Windows.RoutedEventHandler LostFocus
System.Windows.Input.MouseEventHandler LostMouseCapture
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.