EventInfo 类

定义

发现事件的属性并提供对事件元数据的访问权。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)]
[System.Serializable]
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
继承
EventInfo
派生
属性
实现

示例

下面的代码获取EventInfo Button类的Click事件的对象。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.Reflection
Imports System.Security

' 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
End Class

注解

使用类检查事件并挂钩事件处理程序,如AddEventHandler方法的示例代码中所示。 EventInfoUse the EventInfo class to inspect events and to hook up event handlers, as shown in the example code for the AddEventHandler method.

备注

EventInfo不应用于引发事件。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. 引发事件时,将调用事件处理程序委托的 invoke 方法。When the event is raised, the invoke method of the event-handler delegate is called. 支持多播和单强制转换事件通知。Both multicast and single-cast event notifications are supported. AddRemove方法以及与事件关联的事件处理程序委托类必须在元数据中标记为。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. 在 C 或C++中,函数指针是对方法的引用。In C or C++, a function pointer is a reference to a method. 与 C 或C++函数指针不同,委托包含两个引用:对方法的引用和对支持方法的对象的引用。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. 调用委托的 invoke 方法时,只会有一个对象在其上调用方法。When the delegate's invoke method is called, only a single object will have a method called on it. 多路广播修饰符可应用于委托声明,这允许在调用委托的 invoke 方法时调用多个方法。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.

ICustomAttributeProvider.GetCustomAttributes true 如果的参数GetCustomAttributes不遍历类型层次结构,则调用。 inherit EventInfoCalling ICustomAttributeProvider.GetCustomAttributes on EventInfo when the inherit parameter of GetCustomAttributes is true does not walk the type hierarchy. 使用System.Attribute可继承自定义属性。Use System.Attribute to inherit custom attributes.

继承者说明

当从EventInfo继承时,必须重写以下成员: GetAddMethod(Boolean)GetRemoveMethod(Boolean)GetRaiseMethod(Boolean)When you inherit from EventInfo, you must override the following members: GetAddMethod(Boolean), GetRemoveMethod(Boolean), and GetRaiseMethod(Boolean).

构造函数

EventInfo()

初始化 EventInfo 类的新实例。Initializes a new instance of the EventInfo class.

属性

AddMethod

获取事件的 AddEventHandler(Object, Delegate) 方法的 MethodInfo 对象,包括非公共方法。Gets the MethodInfo object for the AddEventHandler(Object, Delegate) method of the event, including non-public methods.

Attributes

获取此事件的属性。Gets the attributes for this event.

CustomAttributes

获取包含此成员自定义属性的集合。Gets a collection that contains this member's custom attributes.

(继承自 MemberInfo)
DeclaringType

获取声明该成员的类。Gets the class that declares this member.

(继承自 MemberInfo)
EventHandlerType

获取与此事件关联的基础事件处理程序委托的 Type 对象。Gets the Type object of the underlying event-handler delegate associated with this event.

IsCollectible

获取一个值,该值指示此 MemberInfo 对象是否是包含在可回收的 AssemblyLoadContext 中的程序集的一部分。Gets a value that indicates whether this MemberInfo object is part of an assembly held in a collectible AssemblyLoadContext.

(继承自 MemberInfo)
IsMulticast

获取一个值,该值指示事件是否为多播。Gets a value indicating whether the event is multicast.

IsSpecialName

获取一个值,该值指示 EventInfo 是否具有一个有特殊意义的名称。Gets a value indicating whether the EventInfo has a name with a special meaning.

MemberType

获取 MemberTypes 值,指示此成员为一个事件。Gets a MemberTypes value indicating that this member is an event.

MetadataToken

获取一个值,该值标识元数据元素。Gets a value that identifies a metadata element.

(继承自 MemberInfo)
Module

获取一个模块,在该模块中已经定义一个类型,该类型用于声明由当前 MemberInfo 表示的成员。Gets the module in which the type that declares the member represented by the current MemberInfo is defined.

(继承自 MemberInfo)
Name

获取当前成员的名称。Gets the name of the current member.

(继承自 MemberInfo)
RaiseMethod

获取引发该事件时所调用的方法,包括非公共方法。Gets the method that is called when the event is raised, including non-public methods.

ReflectedType

获取用于获取 MemberInfo 的此实例的类对象。Gets the class object that was used to obtain this instance of MemberInfo.

(继承自 MemberInfo)
RemoveMethod

获取用于删除事件方法的 MethodInfo 对象,包括非公共方法。Gets the MethodInfo object for removing a method of the event, including non-public methods.

方法

AddEventHandler(Object, Delegate)

将事件处理程序添加到事件源。Adds an event handler to an event source.

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

GetAddMethod()

返回用于向事件源添加事件处理程序委托的方法。Returns the method used to add an event handler delegate to the event source.

GetAddMethod(Boolean)

在派生类中重写时,为事件的 AddEventHandler(Object, Delegate) 方法检索 MethodInfo 对象,指定是否返回非公共方法。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)

在派生类中重写时,返回应用于此成员的所有自定义属性的数组。When overridden in a derived class, returns an array of all custom attributes applied to this member.

(继承自 MemberInfo)
GetCustomAttributes(Type, Boolean)

在派生类中重写时,返回应用于此成员并由 Type 标识的自定义属性的数组。When overridden in a derived class, returns an array of custom attributes applied to this member and identified by Type.

(继承自 MemberInfo)
GetCustomAttributesData()

返回 CustomAttributeData 对象列表,这些对象表示已应用到目标成员的特性相关数据。Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.

(继承自 MemberInfo)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

GetOtherMethods()

返回在元数据中使用 .other 指令与事件相关联的公共方法。Returns the public methods that have been associated with an event in metadata using the .other directive.

GetOtherMethods(Boolean)

返回在元数据中使用 .other 指令与事件相关联的方法,指定是否包括非公共方法。Returns the methods that have been associated with the event in metadata using the .other directive, specifying whether to include non-public methods.

GetRaiseMethod()

返回引发事件时所调用的方法。Returns the method that is called when the event is raised.

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()

返回用于从事件源删除事件处理程序委托的方法。Returns the method used to remove an event handler delegate from the event source.

GetRemoveMethod(Boolean)

在派生类中重写时,检索用于删除事件方法的 MethodInfo 对象,指定是否返回非公共方法。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()
HasSameMetadataDefinitionAs(MemberInfo) (继承自 MemberInfo)
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.

(继承自 MemberInfo)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
RemoveEventHandler(Object, Delegate)

从事件源中删除事件处理程序。Removes an event handler from an event source.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

操作员

Equality(EventInfo, EventInfo)

指示两个 EventInfo 对象是否相等。Indicates whether two EventInfo objects are equal.

Inequality(EventInfo, EventInfo)

指示两个 EventInfo 对象是否不相等。Indicates whether two EventInfo objects are not equal.

显式界面实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

_EventInfo.GetType()

返回表示 EventInfo 类型的 T:System.Type 对象。Returns a T:System.Type object representing the EventInfo type.

_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)

检索对象提供的类型信息接口的数量(0 或 1)。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)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 MemberInfo)
_MemberInfo.GetType()

获取一个表示 MemberInfo 类的 Type 对象。Gets a Type object representing the MemberInfo class.

(继承自 MemberInfo)
_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.

(继承自 MemberInfo)
_MemberInfo.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 MemberInfo)
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 MemberInfo)
ICustomAttributeProvider.GetCustomAttributes(Boolean) (继承自 MemberInfo)
ICustomAttributeProvider.GetCustomAttributes(Type, Boolean) (继承自 MemberInfo)
ICustomAttributeProvider.IsDefined(Type, Boolean) (继承自 MemberInfo)

扩展方法

GetCustomAttribute(MemberInfo, Type)

检索应用于指定成员的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified member.

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)

检索应用于指定成员的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified member.

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)

检索应用于指定成员的自定义特性集合。Retrieves a collection of custom attributes that are applied to a specified member.

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)

检索应用于指定成员的指定类型的自定义特性集合。Retrieves a collection of custom attributes of a specified type that are applied to a specified member.

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)

检索应用于指定成员的指定类型的自定义特性集合。Retrieves a collection of custom attributes of a specified type that are applied to a specified member.

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)

确定是否将指定类型的任何自定义属性应用于指定的成员。Indicates whether custom attributes of a specified type are applied to a specified member.

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, Boolean)
GetRaiseMethod(EventInfo)
GetRaiseMethod(EventInfo, Boolean)
GetRemoveMethod(EventInfo)
GetRemoveMethod(EventInfo, Boolean)
GetMetadataToken(MemberInfo)

获取给定成员的元数据令牌(如果可用)。Gets a metadata token for the given member, if available.

HasMetadataToken(MemberInfo)

返回表示元数据令牌是否可用于指定的成员的值。Returns a value that indicates whether a metadata token is available for the specified member.

安全性

InheritanceDemand
完全信任继承者。for full trust for inheritors. 此类不能由部分信任的代码继承。This class cannot be inherited by partially trusted code.

适用于

线程安全性

此类型是线程安全的。This type is thread safe.