EventInfo 類別

定義

探索事件的屬性並提供事件中繼資料的存取。

public ref class EventInfo abstract : System::Reflection::MemberInfo
public ref class EventInfo abstract : System::Reflection::MemberInfo, System::Runtime::InteropServices::_EventInfo
public abstract class EventInfo : System.Reflection.MemberInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public abstract class EventInfo : 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
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type EventInfo = class
    inherit MemberInfo
    interface _EventInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type EventInfo = class
    inherit MemberInfo
    interface _EventInfo
Public MustInherit Class EventInfo
Inherits MemberInfo
Public MustInherit Class EventInfo
Inherits MemberInfo
Implements _EventInfo
繼承
EventInfo
衍生
屬性
實作

範例

下列程式碼會取得 EventInfoClick 類別事件的物件 Button

#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 範例程式碼所示。

注意

EventInfo 不是用來引發事件。 物件會依照其內部狀態來引發事件。

事件會與委派搭配使用。 事件接聽程式會具現化每當事件來源引發事件時叫用的事件處理常式委派。 為了連接到事件來源,事件接聽程式會將此委派新增至來源上的調用清單。 引發事件時,會呼叫事件處理常式委派的叫用方法。 支援多播和單一轉換事件通知。 AddRemove 方法以及與事件相關聯的事件處理常式委派類別必須在中繼資料中標示。

委派是物件導向函式指標。 在 C 或 C++ 中,函式指標是方法的參考。 相較于 C 或 C++ 函式指標,委派包含兩個參考:方法的參考,以及支援 方法的物件參考。 委派可以叫用方法,而不知道宣告或繼承方法的類別類型。 委派只需要知道方法的傳回類型和參數清單。

事件模型同樣適用于單一轉換和多播委派。 呼叫委派的叫用方法時,只有單一物件會有一個呼叫的方法。 多播修飾詞可以套用至委派宣告,允許呼叫委派的叫用方法時呼叫多個方法。

EventInfo當 的 inherittrueGetCustomAttributes 參數未逐步執行型別階層時,呼叫 。 ICustomAttributeProvider.GetCustomAttributes 使用 System.Attribute 來繼承自訂屬性。

給實施者的注意事項

當您繼承自 EventInfo 時,必須覆寫下列成員: GetAddMethod(Boolean)GetRemoveMethod(Boolean)GetRaiseMethod(Boolean)

建構函式

EventInfo()

初始化 EventInfo 類別的新執行個體。

屬性

AddMethod

取得事件之 MethodInfo 方法 (包括非公用方法) 的 AddEventHandler(Object, Delegate) 物件。

Attributes

取得這個事件的屬性。

CustomAttributes

取得包含此成員之自訂屬性的集合。

(繼承來源 MemberInfo)
DeclaringType

取得宣告這個成員的類別。

(繼承來源 MemberInfo)
EventHandlerType

取得與這個事件相關之目前的事件處理常式委派之 Type 物件。

IsCollectible

取得指出此 MemberInfo 物件是否為可回收 AssemblyLoadContext 中保存之組件一部分的值。

(繼承來源 MemberInfo)
IsMulticast

取得值,指出事件是否為多點傳送。

IsSpecialName

取得值,指出 EventInfo 是否具有特殊意義的名稱。

MemberType

取得 MemberTypes 值,表示這個成員為事件。

MemberType

在衍生類別中覆寫時,取得 MemberTypes 值,指出成員類型︰方法、建構函式、事件等。

(繼承來源 MemberInfo)
MetadataToken

取得值,這個值可識別中繼資料項目。

(繼承來源 MemberInfo)
Module

取得用於定義型別的模組,該型別宣告以目前 MemberInfo 表示的成員。

(繼承來源 MemberInfo)
Name

取得目前成員的名稱。

(繼承來源 MemberInfo)
RaiseMethod

取得引發事件時所呼叫的方法,包括非公用方法。

ReflectedType

取得類別物件,是用來取得這個 MemberInfo 的執行個體。

(繼承來源 MemberInfo)
RemoveMethod

取得用來移除事件方法 (包括非公用方法) 的 MethodInfo 物件。

方法

AddEventHandler(Object, Delegate)

將事件處理常式加入至事件來源。

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 MemberInfo)
GetAddMethod()

傳回用來將事件處理常式委派加入至事件來源的方法。

GetAddMethod(Boolean)

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

GetCustomAttributes(Boolean)

在衍生類別中覆寫時,傳回套用至此成員之所有自訂屬性的陣列。

(繼承來源 MemberInfo)
GetCustomAttributes(Type, Boolean)

當在衍生的類別中覆寫時,會傳回套用至這個成員的自訂屬性陣列,並以 Type 識別。

(繼承來源 MemberInfo)
GetCustomAttributesData()

傳回 CustomAttributeData 物件的清單,表示已套用至目標成員之屬性的資料。

(繼承來源 MemberInfo)
GetHashCode()

傳回這個執行個體的雜湊碼。

GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 MemberInfo)
GetOtherMethods()

傳回使用 .other 指示詞與中繼資料中的事件產生關聯的公用方法。

GetOtherMethods(Boolean)

傳回使用 .other 指示詞與中繼資料中的事件產生關聯的方法,並指定是否要包括非公用方法。

GetRaiseMethod()

傳回引發事件時所呼叫的方法。

GetRaiseMethod(Boolean)

在衍生類別中覆寫時,傳回在引發事件時所呼叫的方法,指定是否要傳回非公用方法。

GetRemoveMethod()

傳回用來從事件來源移除事件處理常式委派的方法。

GetRemoveMethod(Boolean)

在衍生類別中覆寫時,擷取 MethodInfo 物件以移除事件的方法,指定是否要傳回非公用方法。

GetType()

探索類別事件的屬性並提供事件中繼資料的存取。

GetType()

探索成員的屬性並提供成員中繼資料的存取。

(繼承來源 MemberInfo)
HasSameMetadataDefinitionAs(MemberInfo)

探索事件的屬性並提供事件中繼資料的存取。

(繼承來源 MemberInfo)
IsDefined(Type, Boolean)

在衍生類別中覆寫時,表示是否已有一個或多個具有指定型別或其衍生型別的屬性套用至這個成員。

(繼承來源 MemberInfo)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RemoveEventHandler(Object, Delegate)

從事件來源移除事件處理常式。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

運算子

Equality(EventInfo, EventInfo)

表示兩個 EventInfo 物件是否相等。

Inequality(EventInfo, EventInfo)

表示兩個 EventInfo 物件是否不相等。

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

_EventInfo.GetType()

T:System.Type 回 代表型別的 EventInfo 物件。

_EventInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

_EventInfo.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

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

提供物件所公開的屬性和方法的存取權。

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

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

取得 Type 物件,表示 MemberInfo 類別。

(繼承來源 MemberInfo)
_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 MemberInfo)
_MemberInfo.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 MemberInfo)
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 MemberInfo)
ICustomAttributeProvider.GetCustomAttributes(Boolean)

傳回這個成員中定義的所有自訂屬性的陣列 (但具名屬性除外),如果沒有自訂屬性,則傳回空陣列。

(繼承來源 MemberInfo)
ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)

傳回這個成員中定義的自訂屬性陣列 (依類型識別),如果沒有該類型的自訂屬性,則傳回空陣列。

(繼承來源 MemberInfo)
ICustomAttributeProvider.IsDefined(Type, Boolean)

指出此成員上是否有定義一個或多個 attributeType 執行個體。

(繼承來源 MemberInfo)

擴充方法

GetCustomAttribute(MemberInfo, Type)

擷取指定型別的自訂屬性,此屬性套用至指定成員。

GetCustomAttribute(MemberInfo, Type, Boolean)

擷取只訂型別的自訂屬性,此屬性套用至指定成員,並且可選擇性檢查該成員的祖系。

GetCustomAttribute<T>(MemberInfo)

擷取指定型別的自訂屬性,此屬性套用至指定成員。

GetCustomAttribute<T>(MemberInfo, Boolean)

擷取只訂型別的自訂屬性,此屬性套用至指定成員,並且可選擇性檢查該成員的祖系。

GetCustomAttributes(MemberInfo)

擷取套用至指定成員的自訂屬性集合。

GetCustomAttributes(MemberInfo, Boolean)

擷取自訂屬性集合,此集合套用至指定成員,並且可選擇性檢查該成員的祖系。

GetCustomAttributes(MemberInfo, Type)

擷取指定型別的自訂屬性集合,此集合套用至指定成員。

GetCustomAttributes(MemberInfo, Type, Boolean)

擷取指定型別的自訂屬性集合,此集合套用至指定成員,並且可選擇性檢查該成員的祖系。

GetCustomAttributes<T>(MemberInfo)

擷取指定型別的自訂屬性集合,此集合套用至指定成員。

GetCustomAttributes<T>(MemberInfo, Boolean)

擷取指定型別的自訂屬性集合,此集合套用至指定成員,並且可選擇性檢查該成員的祖系。

IsDefined(MemberInfo, Type)

指出是否將所指定型別的自訂屬性套用至指定的成員。

IsDefined(MemberInfo, Type, Boolean)

指出指定之型別的自訂屬性是否會套用至指定的成員,以及選擇性地套用到其上階。

GetAddMethod(EventInfo)

探索事件的屬性並提供事件中繼資料的存取。

GetAddMethod(EventInfo, Boolean)

探索事件的屬性並提供事件中繼資料的存取。

GetRaiseMethod(EventInfo)

探索事件的屬性並提供事件中繼資料的存取。

GetRaiseMethod(EventInfo, Boolean)

探索事件的屬性並提供事件中繼資料的存取。

GetRemoveMethod(EventInfo)

探索事件的屬性並提供事件中繼資料的存取。

GetRemoveMethod(EventInfo, Boolean)

探索事件的屬性並提供事件中繼資料的存取。

GetMetadataToken(MemberInfo)

取得指定成員的中繼資料語彙基元 (如果有)。

HasMetadataToken(MemberInfo)

傳回值,指出所指定成員是否有可用的中繼資料語彙基元。

適用於

執行緒安全性

此型別具備執行緒安全。