Get EventInfo by name and binding flags in CSharp

Description

The following code shows how to get EventInfo by name and binding flags.

Example


//w w w.j a v a  2 s  . c  o m
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());
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type