Type.GetEvent Type.GetEvent Type.GetEvent Type.GetEvent Method

定義

取得由目前 Type 所宣告或繼承的特定事件。Gets a specific event declared or inherited by the current Type.

多載

GetEvent(String) GetEvent(String) GetEvent(String) GetEvent(String)

傳回代表指定公用事件的 EventInfo 物件。Returns the EventInfo object representing the specified public event.

GetEvent(String, BindingFlags) GetEvent(String, BindingFlags) GetEvent(String, BindingFlags) GetEvent(String, BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,傳回表示指定事件的 EventInfo 物件。When overridden in a derived class, returns the EventInfo object representing the specified event, using the specified binding constraints.

GetEvent(String) GetEvent(String) GetEvent(String) GetEvent(String)

傳回代表指定公用事件的 EventInfo 物件。Returns the EventInfo object representing the specified public event.

public:
 virtual System::Reflection::EventInfo ^ GetEvent(System::String ^ name);
public System.Reflection.EventInfo GetEvent (string name);
abstract member GetEvent : string -> System.Reflection.EventInfo
override this.GetEvent : string -> System.Reflection.EventInfo
Public Function GetEvent (name As String) As EventInfo

參數

name
String String String String

字串,其中包含由目前 Type 所宣告或繼承的事件名稱。The string containing the name of an event that is declared or inherited by the current Type.

傳回

如果找到,則表示由目前 Type 所宣告或繼承之指定公用事件的物件,否則為 nullThe object representing the specified public event that is declared or inherited by the current Type, if found; otherwise, null.

實作

例外狀況

範例

下列範例會建立EventInfo物件,並取得指定之事件的按鈕類別的事件。The following example creates an EventInfo object and gets the event for a button class for the specified event.

#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;
      EventInfo^ myEvent = myType->GetEvent( "Click" );
      if ( myEvent != nullptr )
      {
         Console::WriteLine( "Looking for the Click event in the Button class." );
         Console::WriteLine( myEvent );
      }
      else
            Console::WriteLine( "The Click event is not available in 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
        {

            Type myType = typeof(System.Windows.Forms.Button);
            EventInfo myEvent = myType.GetEvent("Click");
            if(myEvent != null)
            {
                Console.WriteLine("Looking for the Click event in the Button class.");
                Console.WriteLine(myEvent.ToString());
            }
            else
                Console.WriteLine("The Click event is not available in 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
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

' 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
            Dim myType As Type = GetType(System.Windows.Forms.Button)
            Dim myEvent As EventInfo = myType.GetEvent("Click")
            If Not (myEvent Is Nothing) Then
                Console.WriteLine(ControlChars.Cr + "Looking for the Click event in the Button class.")
                Console.WriteLine(ControlChars.Cr + myEvent.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 'Main
End Class 'MyEventExample

備註

事件會被視為公用反映,是否至少一個方法或公用存取子。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.

搜尋name會區分大小寫。The search for name is case-sensitive. 搜尋包含 public static 和 public 的執行個體事件。The search includes public static and public instance events.

下表顯示基底類別成員會傳回由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 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.

另請參閱

GetEvent(String, BindingFlags) GetEvent(String, BindingFlags) GetEvent(String, BindingFlags) GetEvent(String, BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,傳回表示指定事件的 EventInfo 物件。When overridden in a derived class, returns the EventInfo object representing the specified event, using the specified binding constraints.

public:
 abstract System::Reflection::EventInfo ^ GetEvent(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo GetEvent (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetEvent : string * System.Reflection.BindingFlags -> System.Reflection.EventInfo
Public MustOverride Function GetEvent (name As String, bindingAttr As BindingFlags) As EventInfo

參數

name
String String String String

字串,包含目前 Type 所宣告或繼承之事件的名稱。The string containing the name of an event which is declared or inherited by the current Type.

bindingAttr
BindingFlags BindingFlags BindingFlags BindingFlags

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

-或--or- 要傳回 nullDefaultDefault to return null.

傳回

如果找到,則表示由目前 Type 所宣告或繼承之指定事件的物件,否則為 nullThe object representing the specified event that is declared or inherited by the current Type, if found; otherwise, null.

實作

例外狀況

範例

下列程式碼範例會使用GetEvent(String, BindingFlags)方法來搜尋的公用或非公用事件名為"Click"不是型別static(Shared Visual Basic 中)。The following code example uses the GetEvent(String, BindingFlags) method to search a type for a public or non-public event named "Click" that is not static (Shared in Visual Basic).

#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
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

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

備註

下列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.

  • 指定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.IgnoreCase 忽略大小寫的nameBindingFlags.IgnoreCase to ignore the case of name.

  • 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 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.

另請參閱

適用於