EventInfo Class discovers the attributes of an event and provides access to event metadata. : Event « Reflection « C# / C Sharp






EventInfo Class discovers the attributes of an event and provides access to event metadata.

  

using System;
using System.Reflection;
using System.Security;

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

   
    
  








Related examples in the same category

1.Show Events
2.Use reflection to get the Elapsed event
3.Get event handler type
4.EventInfo.EventHandlerType
5.Returns the EventInfo object representing the specified public event.
6.Returns the EventInfo object representing the specified event, using the specified binding constraints.
7.Search for events that are declared or inherited by the current Type, using the specified binding constraints.
8.Returns all the public events that are declared or inherited by the current Type.