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

検索を実施する方法を指定する列挙値のビットごとの組み合わせ。

または null を返す場合は Default

戻り値

EventInfo

現在の Type で宣言または継承されている指定イベントが存在する場合は、そのイベントを表すオブジェクト。それ以外の場合は null

実装

例外

namenullです。

次のコード例では、メソッドを使用して、 GetEvent(String, BindingFlags) 型を検索します。このイベントは、 static (Visual Basic) ではなく、"Click" という名前のパブリックイベントまたは非パブリックイベントを検索し Shared ます。

#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.FlattenHierarchy階層の上位にとの静的メンバーを含めるように指定します。 public protected 継承された private クラスの静的メンバーは含まれません。

次の BindingFlags 修飾子フラグを使用して、検索の動作を変更できます。

  • BindingFlags.IgnoreCase の大文字と小文字を区別しない場合は name

  • BindingFlags.DeclaredOnly で宣言されたイベントだけを検索する Type 場合は。単純に継承されたイベントではありません。

詳細については、「System.Reflection.BindingFlags」を参照してください。

パブリックであるメソッドまたはアクセサーが少なくとも1つある場合、イベントはリフレクションに対してパブリックと見なされます。 それ以外の場合、イベントはプライベートと見なされ、 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 し、指定されたイベントの button クラスのイベントを取得します。

#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

注釈

パブリックであるメソッドまたはアクセサーが少なくとも1つある場合、イベントはリフレクションに対してパブリックと見なされます。 それ以外の場合、イベントはプライベートと見なされ、 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basic では、を使用して値を結合 Or ) を使用して取得する必要があります。

の検索で name は、大文字と小文字が区別されます。 検索には、パブリックの静的インスタンスイベントとパブリックインスタンスイベントが含まれます。

次の表は、型に対してリフレクションを行うときに、メソッドによって返される基底クラスのメンバーを示してい Get ます。

メンバーの型 静的 非静的
コンストラクター いいえ いいえ
フィールド いいえ はい。 フィールドは、常に名前と署名で隠ぺいされます。
Event 適用なし 共通型システムの規則は、継承が、プロパティを実装するメソッドと同じであることを示します。 リフレクションは、プロパティを名前で隠す、署名として扱います。 下記のメモ2を参照してください。
メソッド いいえ はい。 メソッド (仮想と非仮想の両方) は、非表示にするか、名前と署名を隠すことができます。
入れ子にされた型 いいえ いいえ
プロパティ 適用なし 共通型システムの規則は、継承が、プロパティを実装するメソッドと同じであることを示します。 リフレクションは、プロパティを名前で隠す、署名として扱います。 下記のメモ2を参照してください。
  1. 名前による隠ぺいと署名では、カスタム修飾子、戻り値の型、パラメーターの型、sentinel、アンマネージ呼び出し規約を含む、シグネチャのすべての部分が考慮されます。 これは、バイナリ比較です。

  2. リフレクションの場合、プロパティとイベントは、名前とシグネチャが隠ぺいされます。 基底クラスに get と set の両方のアクセサーを持つプロパティがあり、派生クラスに get アクセサーのみがある場合、派生クラスのプロパティは基底クラスのプロパティを非表示にします。基底クラスの setter にアクセスすることはできません。

  3. カスタム属性は、共通型システムの一部ではありません。

現在のが Type 構築ジェネリック型を表している場合、このメソッドは、 EventInfo 適切な型引数によって置き換えられた型パラメーターを持つを返します。

現在のが Type ジェネリック型またはジェネリックメソッドの定義の型パラメーターを表している場合、このメソッドはクラス制約のイベントを検索します。

こちらもご覧ください

適用対象