Type.GetEvent 方法

定義

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

多載

GetEvent(String, BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,傳回表示指定事件的 EventInfo 物件。

GetEvent(String)

傳回代表指定公用事件的 EventInfo 物件。

GetEvent(String, BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,傳回表示指定事件的 EventInfo 物件。

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

字串,包含目前 Type 所宣告或繼承之事件的名稱。

bindingAttr
BindingFlags

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

-或- 要傳回 nullDefault

傳回

EventInfo

如果找到,則表示由目前 Type 所宣告或繼承之指定事件的物件,否則為 null

實作

例外狀況

namenull

範例

下列程式碼範例會使用 GetEvent(String, BindingFlags) 方法來搜尋名稱為 "Click" 的公用或非公用事件的類型,而該事件不會 static Shared 在 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.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

備註

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

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

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

  • 指定 BindingFlags.NonPublic 要在搜尋中包含 (的非公用事件,也就是私用、內部和受保護的事件) 。

  • 指定 BindingFlags.FlattenHierarchypublic 在階層中包含和 protected 靜態成員; private 不包含繼承類別中的靜態成員。

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

  • BindingFlags.IgnoreCase 忽略的案例 name

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

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

如果事件至少有一個公用的方法或存取子,則會將事件視為反映的公用。 否則,系統會將事件視為私用,且您必須使用 BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static Visual Basic 中 | | (,並使用 Or) 來合併值以取得它。

如果目前的 Type 代表結構化泛型型別,這個方法會傳回, EventInfo 並以適當的型別引數取代型別參數。

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

另請參閱

適用於

GetEvent(String)

傳回代表指定公用事件的 EventInfo 物件。

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

參數

name
String

字串,其中包含由目前 Type 所宣告或繼承的事件名稱。

傳回

EventInfo

如果找到,則表示由目前 Type 所宣告或繼承之指定公用事件的物件,否則為 null

實作

例外狀況

namenull

範例

下列範例會建立 EventInfo 物件,並取得指定事件之按鈕類別的事件。

#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.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
            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
End Class

備註

如果事件至少有一個公用的方法或存取子,則會將事件視為反映的公用。 否則,系統會將事件視為私用,且您必須使用 BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static Visual Basic 中 | | (,並使用 Or) 來合併值以取得它。

搜尋會區分 name 大小寫。 搜尋包含公用靜態和公用實例事件。

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

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

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

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

如果目前的 Type 代表結構化泛型型別,這個方法會傳回, EventInfo 並以適當的型別引數取代型別參數。

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

另請參閱

適用於