C# Type GetEvent(String, BindingFlags)

Description

Type GetEvent(String, BindingFlags) when overridden in a derived class, returns the EventInfo object representing the specified event, using the specified binding constraints.

Syntax

Type.GetEvent(String, BindingFlags) has the following syntax.


public abstract EventInfo GetEvent(
  string name,
  BindingFlags bindingAttr
)

Parameters

Type.GetEvent(String, BindingFlags) has the following 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.
  • bindingAttr - -or-
  • bindingAttr - Zero, to return null.

Returns

Type.GetEvent(String, BindingFlags) method returns The object representing the specified event that is declared or inherited by the current Type, if found; otherwise, null.

Example

The following code example uses the GetEvent(String, BindingFlags) method to search a type for a public or non-public event named "Click" that is not static (Shared in Visual Basic).


/*w  w w.j a va2  s . c om*/
using System;
using System.Reflection;
using System.Security;

class MyEventExample
{
    public static void Main()
    {  
        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.");
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version