Export (0) Print
Expand All

Type.GetEvents Method (BindingFlags)

.NET Framework 1.1

When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints.

[Visual Basic]
Overloads Public MustOverride Function GetEvents( _
   ByVal bindingAttr As BindingFlags _
) As EventInfo()
[C#]
public abstract EventInfo[] GetEvents(
 BindingFlags bindingAttr
);
[C++]
public: virtual EventInfo* GetEvents(
 BindingFlags bindingAttr
) [] = 0;
[JScript]
public abstract function GetEvents(
   bindingAttr : BindingFlags
) : EventInfo[];

Parameters

bindingAttr
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return a null reference (Nothing in Visual Basic).

Return Value

An array of EventInfo objects representing all events which 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.

Remarks

The following BindingFlags filter flags can be used to define which events to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.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 and protected events) in the search.
  • Specify BindingFlags.FlattenHierarchy to include static members up the hierarchy.

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.

If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).

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] 
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 Language Filter in the upper-left corner of the page.

Requirements

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, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Type Class | Type Members | System Namespace | Type.GetEvents Overload List | EventInfo | BindingFlags | DefaultBinder | ReflectionPermission | GetEvent

Show:
© 2014 Microsoft