Type.GetEvents Type.GetEvents Type.GetEvents Type.GetEvents Method

定義

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

多載

GetEvents() GetEvents() GetEvents() GetEvents()

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

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

傳回

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.

實作

範例

下列範例會取得陣列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
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

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

備註

事件會被視為公用反映,是否至少一個方法或公用存取子。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 StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位永遠是依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件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 和 unmanaged 呼叫慣例。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.

另請參閱

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

參數

bindingAttr
BindingFlags BindingFlags BindingFlags BindingFlags

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

-或--or-

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

傳回

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.

實作

範例

下列範例會取得陣列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
Imports System.Reflection
Imports System.Security
Imports System.Windows.Forms
Imports Microsoft.VisualBasic

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

備註

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.Public来包含在搜尋中的公用事件。Specify BindingFlags.Public to include public events in the search.

  • 指定BindingFlags.NonPublic在搜尋中包含非公用事件 (也就是私用、 內部和受保護的事件)。Specify 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.

另請參閱

適用於