EventInfo EventInfo EventInfo EventInfo Class

Definition

Discovers the attributes of an event and provides access to event metadata.

public ref class EventInfo abstract : System::Reflection::MemberInfo, System::Runtime::InteropServices::_EventInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class EventInfo : System.Reflection.MemberInfo, System.Runtime.InteropServices._EventInfo
type EventInfo = class
    inherit MemberInfo
    interface _EventInfo
Public MustInherit Class EventInfo
Inherits MemberInfo
Implements _EventInfo
Inheritance
Derived
Attributes
Implements

Examples

The following code gets an EventInfo object for the Click event of the Button class.

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.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 = System::Windows::Forms::Button::typeid;
      EventInfo^ myEventBindingFlags = myTypeBindingFlags->GetEvent( "Click", myBindingFlags );
      if ( myEventBindingFlags != nullptr )
      {
         Console::WriteLine( "Looking for the Click event in the Button class with the specified BindingFlags." );
         Console::WriteLine( myEventBindingFlags );
      }
      else
            Console::WriteLine( "The Click event is not available with the Button class." );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "An exception occurred." );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "An exception occurred." );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The following exception was raised : {0}", e->Message );
   }
}
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);
        }
    }
}
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 myEventBindingFlags IsNot 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

Remarks

Use the EventInfo class to inspect events and to hook up event handlers, as shown in the example code for the AddEventHandler method.

Note

EventInfo is not intended to be used to raise events. An object raises events as dictated by its internal state.

Events are used with delegates. An event listener instantiates an event-handler delegate that is invoked whenever the event is raised by an event source. In order to connect to the event source, the event listener adds this delegate to the invocation list on the source. When the event is raised, the invoke method of the event-handler delegate is called. Both multicast and single-cast event notifications are supported. The Add and Remove methods, as well as the event-handler delegate class associated with an event, must be marked in the metadata.

Delegates are object-oriented function pointers. In C or C++, a function pointer is a reference to a method. In contrast to the C or C++ function pointer, a delegate contains two references: a reference to a method and a reference to an object that supports the method. Delegates can invoke a method without knowing the class type that declares or inherits the method. Delegates need only know the return type and parameter list of the method.

The event model works equally well for single-cast and multicast delegates. When the delegate's invoke method is called, only a single object will have a method called on it. A multicast modifier can be applied to a delegate declaration, which allows multiple methods to be called when the invoke method of the delegate is called.

Calling ICustomAttributeProvider.GetCustomAttributes on EventInfo when the inherit parameter of GetCustomAttributes is true does not walk the type hierarchy. Use System.Attribute to inherit custom attributes.

Notes to Inheritors

When you inherit from EventInfo, you must override the following members: GetAddMethod(Boolean), GetRemoveMethod(Boolean), and GetRaiseMethod(Boolean).

Constructors

EventInfo() EventInfo() EventInfo() EventInfo()

Initializes a new instance of the EventInfo class.

Properties

AddMethod AddMethod AddMethod AddMethod

Gets the MethodInfo object for the AddEventHandler(Object, Delegate) method of the event, including non-public methods.

Attributes Attributes Attributes Attributes

Gets the attributes for this event.

CustomAttributes CustomAttributes CustomAttributes CustomAttributes

Gets a collection that contains this member's custom attributes.

(Inherited from MemberInfo)
DeclaringType DeclaringType DeclaringType DeclaringType

Gets the class that declares this member.

(Inherited from MemberInfo)
EventHandlerType EventHandlerType EventHandlerType EventHandlerType

Gets the Type object of the underlying event-handler delegate associated with this event.

IsMulticast IsMulticast IsMulticast IsMulticast

Gets a value indicating whether the event is multicast.

IsSpecialName IsSpecialName IsSpecialName IsSpecialName

Gets a value indicating whether the EventInfo has a name with a special meaning.

MemberType MemberType MemberType MemberType

Gets a MemberTypes value indicating that this member is an event.

MetadataToken MetadataToken MetadataToken MetadataToken

Gets a value that identifies a metadata element.

(Inherited from MemberInfo)
Module Module Module Module

Gets the module in which the type that declares the member represented by the current MemberInfo is defined.

(Inherited from MemberInfo)
Name Name Name Name

Gets the name of the current member.

(Inherited from MemberInfo)
RaiseMethod RaiseMethod RaiseMethod RaiseMethod

Gets the method that is called when the event is raised, including non-public methods.

ReflectedType ReflectedType ReflectedType ReflectedType

Gets the class object that was used to obtain this instance of MemberInfo.

(Inherited from MemberInfo)
RemoveMethod RemoveMethod RemoveMethod RemoveMethod

Gets the MethodInfo object for removing a method of the event, including non-public methods.

Methods

AddEventHandler(Object, Delegate) AddEventHandler(Object, Delegate) AddEventHandler(Object, Delegate) AddEventHandler(Object, Delegate)

Adds an event handler to an event source.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Returns a value that indicates whether this instance is equal to a specified object.

GetAddMethod() GetAddMethod() GetAddMethod() GetAddMethod()

Returns the method used to add an event handler delegate to the event source.

GetAddMethod(Boolean) GetAddMethod(Boolean) GetAddMethod(Boolean) GetAddMethod(Boolean)

When overridden in a derived class, retrieves the MethodInfo object for the AddEventHandler(Object, Delegate) method of the event, specifying whether to return non-public methods.

GetCustomAttributes(Boolean) GetCustomAttributes(Boolean) GetCustomAttributes(Boolean) GetCustomAttributes(Boolean)

When overridden in a derived class, returns an array of all custom attributes applied to this member.

(Inherited from MemberInfo)
GetCustomAttributes(Type, Boolean) GetCustomAttributes(Type, Boolean) GetCustomAttributes(Type, Boolean) GetCustomAttributes(Type, Boolean)

When overridden in a derived class, returns an array of custom attributes applied to this member and identified by Type.

(Inherited from MemberInfo)
GetCustomAttributesData() GetCustomAttributesData() GetCustomAttributesData() GetCustomAttributesData()

Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.

(Inherited from MemberInfo)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Returns the hash code for this instance.

GetOtherMethods() GetOtherMethods() GetOtherMethods() GetOtherMethods()

Returns the public methods that have been associated with an event in metadata using the .other directive.

GetOtherMethods(Boolean) GetOtherMethods(Boolean) GetOtherMethods(Boolean) GetOtherMethods(Boolean)

Returns the methods that have been associated with the event in metadata using the .other directive, specifying whether to include non-public methods.

GetRaiseMethod() GetRaiseMethod() GetRaiseMethod() GetRaiseMethod()

Returns the method that is called when the event is raised.

GetRaiseMethod(Boolean) GetRaiseMethod(Boolean) GetRaiseMethod(Boolean) GetRaiseMethod(Boolean)

When overridden in a derived class, returns the method that is called when the event is raised, specifying whether to return non-public methods.

GetRemoveMethod() GetRemoveMethod() GetRemoveMethod() GetRemoveMethod()

Returns the method used to remove an event handler delegate from the event source.

GetRemoveMethod(Boolean) GetRemoveMethod(Boolean) GetRemoveMethod(Boolean) GetRemoveMethod(Boolean)

When overridden in a derived class, retrieves the MethodInfo object for removing a method of the event, specifying whether to return non-public methods.

GetType() GetType() GetType() GetType()
HasSameMetadataDefinitionAs(MemberInfo) HasSameMetadataDefinitionAs(MemberInfo) HasSameMetadataDefinitionAs(MemberInfo) HasSameMetadataDefinitionAs(MemberInfo) Inherited from MemberInfo
IsDefined(Type, Boolean) IsDefined(Type, Boolean) IsDefined(Type, Boolean) IsDefined(Type, Boolean)

When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member.

(Inherited from MemberInfo)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
RemoveEventHandler(Object, Delegate) RemoveEventHandler(Object, Delegate) RemoveEventHandler(Object, Delegate) RemoveEventHandler(Object, Delegate)

Removes an event handler from an event source.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Operators

Equality(EventInfo, EventInfo) Equality(EventInfo, EventInfo) Equality(EventInfo, EventInfo) Equality(EventInfo, EventInfo)

Indicates whether two EventInfo objects are equal.

Inequality(EventInfo, EventInfo) Inequality(EventInfo, EventInfo) Inequality(EventInfo, EventInfo) Inequality(EventInfo, EventInfo)

Indicates whether two EventInfo objects are not equal.

Explicit Interface Implementations

ICustomAttributeProvider.GetCustomAttributes(Boolean) ICustomAttributeProvider.GetCustomAttributes(Boolean) ICustomAttributeProvider.GetCustomAttributes(Boolean) ICustomAttributeProvider.GetCustomAttributes(Boolean) Inherited from MemberInfo
ICustomAttributeProvider.GetCustomAttributes(Type, Boolean) ICustomAttributeProvider.GetCustomAttributes(Type, Boolean) ICustomAttributeProvider.GetCustomAttributes(Type, Boolean) ICustomAttributeProvider.GetCustomAttributes(Type, Boolean) Inherited from MemberInfo
ICustomAttributeProvider.IsDefined(Type, Boolean) ICustomAttributeProvider.IsDefined(Type, Boolean) ICustomAttributeProvider.IsDefined(Type, Boolean) ICustomAttributeProvider.IsDefined(Type, Boolean) Inherited from MemberInfo
_EventInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _EventInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _EventInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _EventInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

_EventInfo.GetType() _EventInfo.GetType() _EventInfo.GetType() _EventInfo.GetType()

Returns a T:System.Type object representing the EventInfo type.

_EventInfo.GetTypeInfo(UInt32, UInt32, IntPtr) _EventInfo.GetTypeInfo(UInt32, UInt32, IntPtr) _EventInfo.GetTypeInfo(UInt32, UInt32, IntPtr) _EventInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.

_EventInfo.GetTypeInfoCount(UInt32) _EventInfo.GetTypeInfoCount(UInt32) _EventInfo.GetTypeInfoCount(UInt32) _EventInfo.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

_EventInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _EventInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _EventInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _EventInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

_MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from MemberInfo)
_MemberInfo.GetType() _MemberInfo.GetType() _MemberInfo.GetType() _MemberInfo.GetType()

Gets a Type object representing the MemberInfo class.

(Inherited from MemberInfo)
_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr) _MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr) _MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr) _MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.

(Inherited from MemberInfo)
_MemberInfo.GetTypeInfoCount(UInt32) _MemberInfo.GetTypeInfoCount(UInt32) _MemberInfo.GetTypeInfoCount(UInt32) _MemberInfo.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from MemberInfo)
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

(Inherited from MemberInfo)

Extension Methods

GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type)

Retrieves a custom attribute of a specified type that is applied to a specified member.

GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean)

Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttribute<T>(MemberInfo) GetCustomAttribute<T>(MemberInfo) GetCustomAttribute<T>(MemberInfo) GetCustomAttribute<T>(MemberInfo)

Retrieves a custom attribute of a specified type that is applied to a specified member.

GetCustomAttribute<T>(MemberInfo, Boolean) GetCustomAttribute<T>(MemberInfo, Boolean) GetCustomAttribute<T>(MemberInfo, Boolean) GetCustomAttribute<T>(MemberInfo, Boolean)

Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttributes(MemberInfo) GetCustomAttributes(MemberInfo) GetCustomAttributes(MemberInfo) GetCustomAttributes(MemberInfo)

Retrieves a collection of custom attributes that are applied to a specified member.

GetCustomAttributes(MemberInfo, Boolean) GetCustomAttributes(MemberInfo, Boolean) GetCustomAttributes(MemberInfo, Boolean) GetCustomAttributes(MemberInfo, Boolean)

Retrieves a collection of custom attributes that are applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttributes(MemberInfo, Type) GetCustomAttributes(MemberInfo, Type) GetCustomAttributes(MemberInfo, Type) GetCustomAttributes(MemberInfo, Type)

Retrieves a collection of custom attributes of a specified type that are applied to a specified member.

GetCustomAttributes(MemberInfo, Type, Boolean) GetCustomAttributes(MemberInfo, Type, Boolean) GetCustomAttributes(MemberInfo, Type, Boolean) GetCustomAttributes(MemberInfo, Type, Boolean)

Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttributes<T>(MemberInfo) GetCustomAttributes<T>(MemberInfo) GetCustomAttributes<T>(MemberInfo) GetCustomAttributes<T>(MemberInfo)

Retrieves a collection of custom attributes of a specified type that are applied to a specified member.

GetCustomAttributes<T>(MemberInfo, Boolean) GetCustomAttributes<T>(MemberInfo, Boolean) GetCustomAttributes<T>(MemberInfo, Boolean) GetCustomAttributes<T>(MemberInfo, Boolean)

Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member.

IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type)

Indicates whether custom attributes of a specified type are applied to a specified member.

IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean)

Indicates whether custom attributes of a specified type are applied to a specified member, and, optionally, applied to its ancestors.

GetAddMethod(EventInfo) GetAddMethod(EventInfo) GetAddMethod(EventInfo) GetAddMethod(EventInfo)
GetAddMethod(EventInfo, Boolean) GetAddMethod(EventInfo, Boolean) GetAddMethod(EventInfo, Boolean) GetAddMethod(EventInfo, Boolean)
GetRaiseMethod(EventInfo) GetRaiseMethod(EventInfo) GetRaiseMethod(EventInfo) GetRaiseMethod(EventInfo)
GetRaiseMethod(EventInfo, Boolean) GetRaiseMethod(EventInfo, Boolean) GetRaiseMethod(EventInfo, Boolean) GetRaiseMethod(EventInfo, Boolean)
GetRemoveMethod(EventInfo) GetRemoveMethod(EventInfo) GetRemoveMethod(EventInfo) GetRemoveMethod(EventInfo)
GetRemoveMethod(EventInfo, Boolean) GetRemoveMethod(EventInfo, Boolean) GetRemoveMethod(EventInfo, Boolean) GetRemoveMethod(EventInfo, Boolean)
GetMetadataToken(MemberInfo) GetMetadataToken(MemberInfo) GetMetadataToken(MemberInfo) GetMetadataToken(MemberInfo)
HasMetadataToken(MemberInfo) HasMetadataToken(MemberInfo) HasMetadataToken(MemberInfo) HasMetadataToken(MemberInfo)

Security

InheritanceDemand
for full trust for inheritors. This class cannot be inherited by partially trusted code.

Applies to

Thread Safety

This type is thread safe.