Type.GetEvents 方法

定義

取得由目前 Type 所宣告或繼承的事件。

多載

GetEvents()

取得由目前 Type 所宣告或繼承的所有公用事件。

GetEvents(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋由目前 Type 所宣告或繼承的事件。

GetEvents()

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

取得由目前 Type 所宣告或繼承的所有公用事件。

public:
 virtual cli::array <System::Reflection::EventInfo ^> ^ GetEvents();
public virtual System.Reflection.EventInfo[] GetEvents ();
abstract member GetEvents : unit -> System.Reflection.EventInfo[]
override this.GetEvents : unit -> System.Reflection.EventInfo[]
Public Overridable Function GetEvents () As EventInfo()

傳回

EventInfo 物件的陣列,代表目前 Type 所宣告或繼承的所有公用事件。

-或-

EventInfo 類型的空陣列 (如果目前 Type 沒有公用事件)。

實作

範例

下列範例會取得 物件的陣列 EventInfo 、取得類別的所有事件 Button ,並顯示事件名稱。 若要編譯 Visual Basic 範例,請使用下列命令列:

vbc type_getevents1.vb /r:System.Windows.Forms.dll /r:System.dll

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Security;

int main()
{
   try
   {
      Type^ myType = System::Windows::Forms::Button::typeid;
      array<EventInfo^>^myEvents = myType->GetEvents();
      Console::WriteLine( "The events on the Button class are: " );
      for ( int index = 0; index < myEvents->Length; index++ )
      {
         Console::WriteLine( myEvents[ index ] );

      }
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
using System;
using System.Reflection;
using System.Security;

class EventsSample
{
    public static void Main()
    {
        try
        {
            // Creates a bitmask based on BindingFlags.
            BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public;
            Type myTypeEvent = typeof(System.Windows.Forms.Button);
            EventInfo[] myEventsBindingFlags = myTypeEvent.GetEvents(myBindingFlags);
            Console.WriteLine("\nThe events on the Button class with the specified BindingFlags are : ");
            for (int index = 0; index < myEventsBindingFlags.Length; index++)
            {
                Console.WriteLine(myEventsBindingFlags[index].ToString());
            }
        }
        catch(SecurityException e)
        {
            Console.WriteLine("SecurityException :" + e.Message);
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException : " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception : " + e.Message);
        }
    }
}
open System
open System.Reflection
open System.Security

try
    // Creates a bitmask based on BindingFlags.
    let myBindingFlags = BindingFlags.Instance ||| BindingFlags.Public
    let myTypeEvent = typeof<System.Windows.Forms.Button>
    let myEventsBindingFlags = myTypeEvent.GetEvents myBindingFlags
    printfn "\nThe events on the Button class with the specified BindingFlags are : "
    for flag in myEventsBindingFlags do
        printfn $"{flag}"
with
| :? SecurityException as e ->
    printfn $"SecurityException: {e.Message}"
| :? ArgumentNullException as e ->
    printfn $"ArgumentNullException: {e.Message}"
| e ->
    printfn $"Exception : {e.Message}"
Imports System.Reflection
Imports System.Security

' Compile this sample using the following command line:
' vbc type_getevents.vb /r:"System.Windows.Forms.dll" /r:"System.dll"

Class EventsSample

    Public Shared Sub Main()
        Try
            ' Creates a bitmask based on BindingFlags.
            Dim myBindingFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.Public
            Dim myTypeEvent As Type = GetType(System.Windows.Forms.Button)
            Dim myEventsBindingFlags As EventInfo() = myTypeEvent.GetEvents(myBindingFlags)
            Console.WriteLine(ControlChars.Cr + "The events on the Button class with the specified BindingFlags are : ")
            Dim index As Integer
            For index = 0 To myEventsBindingFlags.Length - 1
                Console.WriteLine(myEventsBindingFlags(index).ToString())
            Next index
        Catch e As SecurityException
            Console.WriteLine(("SecurityException :" + e.Message))
        Catch e As ArgumentNullException
            Console.WriteLine(("ArgumentNullException : " + e.Message))
        Catch e As Exception
            Console.WriteLine(("Exception : " + e.Message))
        End Try
    End Sub
End Class

備註

如果事件至少有一個公用的方法或存取子是公用的,則會將其視為公用。 否則,事件會被視為私用,而且您必須在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,請使用 Or) 來合併值來取得它。

在 .NET 6 和舊版中 GetEvents ,方法不會以特定順序傳回事件,例如字母順序或宣告順序。 您的程式碼不得依存于傳回事件的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的中繼資料排序來確定。

這個方法可由衍生類別覆寫。

下表顯示反映類型時,方法會傳 Get 回基類的成員。

成員類型 Static 非靜態
建構函式
欄位 可以。 欄位一律會依名稱與簽章隱藏。
事件 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附注 2。
方法 可以。 (虛擬和非虛擬) 的方法可以是依名稱隱藏或依名稱隱藏和簽章。
巢狀類型
屬性 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附注 2。
  1. 隱藏名稱與簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回型別、參數類型、sentinels 和 Unmanaged 呼叫慣例。 這是二進位比較。

  2. 對於反映,屬性和事件會依名稱與簽章隱藏。 如果您的屬性同時具有基類中的 get 和 set 存取子,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類上的 setter。

  3. 自訂屬性不是一般型別系統的一部分。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 EventInfo 物件,其類型參數會由適當的型別引數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的事件。

另請參閱

適用於

GetEvents(BindingFlags)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋由目前 Type 所宣告或繼承的事件。

public:
 abstract cli::array <System::Reflection::EventInfo ^> ^ GetEvents(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo[] GetEvents (System.Reflection.BindingFlags bindingAttr);
abstract member GetEvents : System.Reflection.BindingFlags -> System.Reflection.EventInfo[]
Public MustOverride Function GetEvents (bindingAttr As BindingFlags) As EventInfo()

參數

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回空陣列的 Default

傳回

EventInfo 物件的陣列,代表目前 Type 所宣告或繼承之符合指定繫結條件約束的所有事件。

-或-

如果目前 EventInfo 沒有事件,或沒有事件符合繫結條件約束,則為 Type 類型的空陣列。

實作

範例

下列範例會取得符合指定系結旗標的物件陣列 EventInfo 、取得類別的所有事件 Button ,以及顯示事件名稱。 若要編譯 Visual Basic 範例,請使用下列命令列:

vbc type_getevents2.vb /r:System.Windows.Forms.dll /r:System.dll

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Security;

int main()
{
   try
   {
      
      // Create a bitmask based on BindingFlags.
      BindingFlags myBindingFlags = static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public);
      Type^ myTypeEvent = System::Windows::Forms::Button::typeid;
      array<EventInfo^>^myEventsBindingFlags = myTypeEvent->GetEvents( myBindingFlags );
      Console::WriteLine( "\nThe events on the Button class with the specified BindingFlags are:" );
      for ( int index = 0; index < myEventsBindingFlags->Length; index++ )
      {
         Console::WriteLine( myEventsBindingFlags[ index ] );

      }
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
using System;
using System.Reflection;
using System.Security;

class EventsSample
{
    public static void Main()
    {
        try
        {
            // Create a bitmask based on BindingFlags.
            BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public;
            Type myTypeEvent = typeof(System.Windows.Forms.Button);
            EventInfo[] myEventsBindingFlags = myTypeEvent.GetEvents(myBindingFlags);
            Console.WriteLine("\nThe events on the Button class with the specified BindingFlags are:");
            for (int index = 0; index < myEventsBindingFlags.Length; index++)
            {
                Console.WriteLine(myEventsBindingFlags[index].ToString());
            }
        }
        catch(SecurityException e)
        {
            Console.WriteLine("SecurityException:" + e.Message);
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}
open System
open System.Reflection
open System.Security

try
    // Creates a bitmask based on BindingFlags.
    let myBindingFlags = BindingFlags.Instance ||| BindingFlags.Public
    let myTypeEvent = typeof<System.Windows.Forms.Button>
    let myEventsBindingFlags = myTypeEvent.GetEvents myBindingFlags
    printfn "\nThe events on the Button class with the specified BindingFlags are : "
    for flag in myEventsBindingFlags do
        printfn $"{flag}"
with
| :? SecurityException as e ->
    printfn $"SecurityException: {e.Message}"
| :? ArgumentNullException as e ->
    printfn $"ArgumentNullException: {e.Message}"
| e ->
    printfn $"Exception : {e.Message}"
Imports System.Reflection
Imports System.Security
Imports System.Windows.Forms

Class EventsSample

    Public Shared Sub Main()
        Try
            ' Create a bitmask based on BindingFlags.
            Dim myBindingFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.Public
            Dim myTypeEvent As Type = GetType(System.Windows.Forms.Button)
            Dim myEventsBindingFlags As EventInfo() = myTypeEvent.GetEvents(myBindingFlags)
            Console.WriteLine(ControlChars.Cr + "The events on the Button class with the specified BindingFlags are:")
            Dim index As Integer
            For index = 0 To myEventsBindingFlags.Length - 1
                Console.WriteLine(myEventsBindingFlags(index).ToString())
            Next index
        Catch e As SecurityException
            Console.WriteLine("SecurityException:" + e.Message)
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " + e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message)
        End Try
    End Sub
End Class

備註

在 .NET 6 和舊版中 GetEvents ,方法不會以特定順序傳回事件,例如字母順序或宣告順序。 您的程式碼不得依存于傳回事件的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的中繼資料排序來確定。

下列 BindingFlags 篩選旗標可用來定義要包含在搜尋中的事件:

  • 您必須指定 BindingFlags.InstanceBindingFlags.Static ,才能取得傳回。

  • 指定 BindingFlags.Public 以在搜尋中包含公用事件。

  • 指定 BindingFlags.NonPublic 以在搜尋中包含非公用事件 (,也就是私人、內部和受保護的) 事件。 只會傳回基類上的受保護和內部事件;不會傳回基類上的私用事件。

  • 指定要 BindingFlags.FlattenHierarchy 在階層中加入 publicprotected 靜態成員; private 繼承類別中的靜態成員不包含在內。

下列 BindingFlags 修飾詞旗標可用來變更搜尋的運作方式:

  • BindingFlags.DeclaredOnly 只搜尋 在 上 Type 宣告的事件,而不是只繼承的事件。

如需相關資訊,請參閱 System.Reflection.BindingFlags

如果事件至少有一個公用的方法或存取子是公用的,則會將其視為公用。 否則,事件會被視為私用,而且您必須在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,請使用 Or) 來合併值來取得它。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 EventInfo 物件,其類型參數會由適當的型別引數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的事件。

另請參閱

適用於