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
衍生
屬性
實作

範例

下列程式碼會針對 Button 類別的 Click 事件取得 EventInfo 物件。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

備註

使用 EventInfo 類別來檢查事件,並連結事件處理常式,如 AddEventHandler 方法的範例程式碼所示。Use 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. 支援多播和單一 cast 事件通知。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.

GetCustomAttributesinherit 參數為 true 時,在 EventInfo 上呼叫 ICustomAttributeProvider.GetCustomAttributes 不會逐步執行類型階層。Calling 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

取得事件之 MethodInfo 方法 (包括非公用方法) 的 AddEventHandler(Object, Delegate) 物件。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)

在衍生類別中覆寫時,針對事件的 MethodInfo 方法擷取 AddEventHandler(Object, Delegate) 物件,指定是否要傳回非公用方法。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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

_EventInfo.GetType()

傳回表示 類型的T:System.TypeEventInfo 物件。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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 MemberInfo)
_MemberInfo.GetType()

取得 Type 物件,表示 MemberInfo 類別。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.