EventInfo Class

System.Reflection

Represents an event and provides access to its metadata.

Syntax

public abstract class EventInfo : MemberInfo

Inheritance Hierarchy

Object
   MemberInfo
      EventInfo

Constructors

No public constructors are available for this class. This class cannot be inherited by users.

Properties

AddMethod
Gets the method used to add an event handler to the event.
EventHandlerType
Gets the delegate type for the event.
IsMulticast
Gets a value that indicates whether the event is multicast.
Name
Gets the name of the member.
DeclaringType
Gets the class that logically contains the member.
ReflectedType
Gets the class that is used to perform the reflection, and that has been used to get the member.
MetadataToken
Gets the metadata token for the current `MemberInfo`.
RaiseMethod
Gets the method that is used to raise the event.
RemoveMethod
Gets the method used to remove an event handler from the event.

Methods

AddEventHandler(Object target, Delegate handler)
Adds an event handler to the event.
Equals(Object obj)
Determines whether the specified object is equal to the current object.
GetAddMethod(Boolean nonPublic)
Gets the public or non-public add method for the event.
GetBaseDefinition()
Returns the EventInfo object that indicates the public ביותר event in the inheritance chain. This method may not return the correct results for events declared in certain types of generic types.
GetHashCode()
Serves as the default hash function.
GetMethodList(Boolean nonPublic)
Returns a list of the public or non-public methods of the event.
GetRemoveMethod(Boolean nonPublic)
Gets the public or non-public remove method for the event.
GetType()
Gets the Type of the current instance.
RemoveEventHandler(Object target, Delegate handler)
Removes an event handler from the event.
ToString()
Returns a string that represents the current object.

Remarks

The `EventInfo` class is used to inspect event declarations at run time. It allows you to retrieve information about the event, such as its name, declaring type, and the methods associated with adding or removing event handlers.

This class is part of the System.Reflection namespace, which provides types that allow late-bound access and inspection of a loaded assembly during run time.

Important: `EventInfo` objects are typically obtained through reflection, for example, by calling `Type.GetEvents()` or `Type.GetEvent(string name)`.

Examples

The following example demonstrates how to retrieve and display information about events declared in a class.


using System;
using System.Reflection;

public class MyEvents
{
    public event EventHandler MyCustomEvent;
    public event Action AnotherEvent;

    protected virtual void OnMyCustomEvent(EventArgs e)
    {
        MyCustomEvent?.Invoke(this, e);
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Type myEventsType = typeof(MyEvents);
        EventInfo[] events = myEventsType.GetEvents();

        Console.WriteLine($"Events declared in {myEventsType.Name}:");
        foreach (EventInfo ev in events)
        {
            Console.WriteLine($"  Name: {ev.Name}");
            Console.WriteLine($"  Handler Type: {ev.EventHandlerType.Name}");
            Console.WriteLine($"  Is Multicast: {ev.IsMulticast}");

            MethodInfo addMethod = ev.GetAddMethod(true); // Get non-public add method
            if (addMethod != null)
            {
                Console.WriteLine($"  Add Method: {addMethod.Name}");
            }

            MethodInfo removeMethod = ev.GetRemoveMethod(true); // Get non-public remove method
            if (removeMethod != null)
            {
                Console.WriteLine($"  Remove Method: {removeMethod.Name}");
            }
            Console.WriteLine();
        }
    }
}