Export (0) Print
Expand All

Type.GetEvent Method (String, BindingFlags)

.NET Framework 1.1

When overridden in a derived class, returns the EventInfo object representing the specified event, using the specified binding constraints.

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

Parameters

name
The String containing the name of an event which is declared or inherited by the current Type.
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

The EventInfo object representing the specified event which is declared or inherited by the current Type, if found; otherwise, a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
ArgumentNullException name is a null reference (Nothing in Visual Basic).

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.IgnoreCase to ignore the case of name.
  • 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] 
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 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.GetEvent Overload List | EventInfo | String | BindingFlags | DefaultBinder | ReflectionPermission | GetEvents

Show:
© 2014 Microsoft