Type.GetEvents Method

Definition

取得由目前 Type 所宣告或繼承的事件。Gets the events that are declared or inherited by the current Type.

Overloads

GetEvents()

取得由目前 Type 所宣告或繼承的所有公用事件。Returns all the public events that are declared or inherited by the current Type.

GetEvents(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋由目前 Type 所宣告或繼承的事件。When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints.

GetEvents()

取得由目前 Type 所宣告或繼承的所有公用事件。Returns all the public events that are declared or inherited by the current 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()

Returns

EventInfo[]

EventInfo 物件的陣列,代表目前 Type 所宣告或繼承的所有公用事件。An array of EventInfo objects representing all the public events which are declared or inherited by the current Type.

-或--or- EventInfo 類型的空陣列 (如果目前 Type 沒有公用事件)。An empty array of type EventInfo, if the current Type does not have public events.

Implements

Examples

下列範例會取得 EventInfo 物件的陣列、取得 Button 類別的所有事件,並顯示事件名稱。The following example obtains an array of EventInfo objects, gets all the events for a Button class, and displays the event names. 若要編譯 Visual Basic 範例,請使用下列命令列:To compile the Visual Basic example, use the following command line:

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);
        }
    }
}
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

Remarks

如果事件至少有一個是公用的方法或存取子,則會將事件視為反映的公用。An event is considered public to reflection if it has at least one method or accessor that is public. 否則,事件會被視為私用,而且您必須|使用| BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static (在 Visual Basic 中,使用 Or來結合值)來取得它。Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

GetEvents 方法不會以特定順序傳回事件,例如依字母順序或宣告順序。The GetEvents method does not return events in a particular order, such as alphabetical or declaration order. 您的程式碼不能相依于傳回事件的順序,因為該順序會有所不同。Your code must not depend on the order in which events are returned, because that order varies.

衍生類別可以覆寫這個方法。This method can be overridden by a derived class.

下表顯示當在類型上反映時,Get 方法傳回的基類成員。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type 靜態Static 非靜態Non-Static
建構函式Constructor No No
欄位Field No 是。Yes. 欄位一律會依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
事件Event 不適用Not applicable 一般類型系統規則是,繼承與實作為屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會將屬性視為逐名稱和簽章。Reflection treats properties as hide-by-name-and-signature. 請參閱下面的注2。See note 2 below.
方法Method No 是。Yes. 方法(虛擬和非虛擬)可以依名稱或逐名稱和簽章來隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀型別Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是,繼承與實作為屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會將屬性視為逐名稱和簽章。Reflection treats properties as hide-by-name-and-signature. 請參閱下面的注2。See note 2 below.
  1. [逐名稱] 和 [簽章] 會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、sentinels 和非受控呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 針對反映,屬性和事件是依名稱和簽章來隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您在基類中具有具有 get 和 set 存取子的屬性,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類上的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不是一般型別系統的一部分。Custom attributes are not part of the common type system.

如果目前的 Type 代表結構化的泛型型別,這個方法會傳回具有型別參數的 EventInfo 物件,並由適當的型別引數取代。If the current Type represents a constructed generic type, this method returns the EventInfo objects with the type parameters replaced by the appropriate type arguments.

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的事件。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the events of the class constraint.

See also

GetEvents(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋由目前 Type 所宣告或繼承的事件。When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints.

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

Parameters

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or-

要傳回空陣列的 DefaultDefault to return an empty array.

Returns

EventInfo[]

EventInfo 物件的陣列,代表目前 Type 所宣告或繼承之符合指定繫結條件約束的所有事件。An array of EventInfo objects representing all events that are declared or inherited by the current Type that match the specified binding constraints.

-或--or- 如果目前 EventInfo 沒有事件,或沒有事件符合繫結條件約束,則為 Type 類型的空陣列。An empty array of type EventInfo, if the current Type does not have events, or if none of the events match the binding constraints.

Implements

Examples

下列範例會取得符合指定系結旗標之 EventInfo 物件的陣列、取得 Button 類別的所有事件,並顯示事件名稱。The following example obtains an array of EventInfo objects that match the specified binding flags, gets all the events for a Button class, and displays the event names. 若要編譯 Visual Basic 範例,請使用下列命令列:To compile the Visual Basic example, use the following command line:

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);
        }
    }
}
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

Remarks

GetEvents 方法不會以特定順序傳回事件,例如依字母順序或宣告順序。The GetEvents method does not return events in a particular order, such as alphabetical or declaration order. 您的程式碼不能相依于傳回事件的順序,因為該順序會有所不同。Your code must not depend on the order in which events are returned, because that order varies.

下列 BindingFlags 篩選旗標可以用來定義要包含在搜尋中的事件:The following BindingFlags filter flags can be used to define which events to include in the search:

  • 您必須指定 BindingFlags.InstanceBindingFlags.Static,才能取得傳回。You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定要在搜尋中包含公用事件 BindingFlags.PublicSpecify BindingFlags.Public to include public events in the search.

  • 指定要在搜尋中包含非公用事件(也就是私用、內部和受保護事件) BindingFlags.NonPublicSpecify BindingFlags.NonPublic to include non-public events (that is, private, internal, and protected events) in the search. 只會傳回基類上受保護和內部的事件;不會傳回基類上的私用事件。Only protected and internal events on base classes are returned; private events on base classes are not returned.

  • 指定 BindingFlags.FlattenHierarchy 在階層中包含 publicprotected 靜態成員;不包含繼承類別中 private 的靜態成員。Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

下列 BindingFlags 的修飾詞旗標可以用來變更搜尋的運作方式:The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly,只搜尋 Type上宣告的事件,而不是直接繼承的事件。BindingFlags.DeclaredOnly to search only the events declared on the Type, not events that were simply inherited.

如需相關資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

如果事件至少有一個是公用的方法或存取子,則會將事件視為反映的公用。An event is considered public to reflection if it has at least one method or accessor that is public. 否則,事件會被視為私用,而且您必須|使用| BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static (在 Visual Basic 中,使用 Or來結合值)來取得它。Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

如果目前的 Type 代表結構化的泛型型別,這個方法會傳回具有型別參數的 EventInfo 物件,並由適當的型別引數取代。If the current Type represents a constructed generic type, this method returns the EventInfo objects with the type parameters replaced by the appropriate type arguments.

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的事件。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the events of the class constraint.

See also

Applies to