Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Type.GetEvent Method

Gets a specific event declared or inherited by the current Type.

Overload List

Returns the EventInfo object representing the specified event.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function GetEvent(String) As EventInfo
[C#] public EventInfo GetEvent(string);
[C++] public: EventInfo* GetEvent(String*);
[JScript] public function GetEvent(String) : EventInfo;

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

Supported by the .NET Compact Framework.

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

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetEvent. For other examples that might be available, see the individual overload topics.
[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.

See Also

Type Class | Type Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.