Type.GetEventとは? わかりやすく解説

Type.GetEvent メソッド


Type.GetEvent メソッド (String, BindingFlags)

派生クラスによってオーバーライドされた場合、指定したバインディング制約を使用して、指定したイベントを表す EventInfo オブジェクトを返します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public MustOverride Function
 GetEvent ( _
    name As String, _
    bindingAttr As BindingFlags _
) As EventInfo
Dim instance As Type
Dim name As String
Dim bindingAttr As BindingFlags
Dim returnValue As EventInfo

returnValue = instance.GetEvent(name, bindingAttr)
public abstract EventInfo GetEvent (
    string name,
    BindingFlags bindingAttr
)
public:
virtual EventInfo^ GetEvent (
    String^ name, 
    BindingFlags bindingAttr
) abstract
public abstract EventInfo GetEvent (
    String name, 
    BindingFlags bindingAttr
)
public abstract function GetEvent (
    name : String, 
    bindingAttr : BindingFlags
) : EventInfo

パラメータ

name

現在の Type で宣言または継承されているイベントの名前を格納する String。

bindingAttr

検索の実行方法を指定する 1 つ以上の BindingFlags から成るビット マスク。

または

null 参照 (Visual Basic では Nothing) を返す 0。

戻り値
現在の Type で宣言または継承されている指定イベントが存在する場合は、そのイベントを表す EventInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentNullException

name が null 参照 (Visual Basic では Nothing) です。

解説解説

次の BindingFlags フィルタ フラグは、検索対象に含めるイベントを定義するために使用できます。

次の BindingFlags 修飾フラグは、検索方法を変更するために使用できます。

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

要求された型がパブリックではなく、呼び出し元に現在のアセンブリ外の非パブリック オブジェクトをリフレクションするための ReflectionPermission がない場合、このメソッドは null 参照 (Visual Basic では Nothing) を返します。

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

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

使用例使用例
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 Not (myEventBindingFlags Is
 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
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);
        }
    }
}
#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
 );
   }
}
import System.*;
import System.Reflection.*;
import System.Security.*;

class MyEventExample
{
    public static void main(String[]
 args)
    {
        try {
            // Creates a bitmask based on BindingFlags.
            BindingFlags myBindingFlags = BindingFlags.Instance
                | BindingFlags.Public | BindingFlags.NonPublic;
            Type myTypeBindingFlags = System.Windows.Forms.Button.class.ToType();
            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.get_Message());
        }
        catch (ArgumentNullException e) {
            Console.WriteLine("An exception occurred.");
            Console.WriteLine("Message :" + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("The following exception was raised : {0}"
,
                e.get_Message());
        }
    } //main
} //MyEventExample
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
EventInfo
String クラス
BindingFlags
DefaultBinder
ReflectionPermission
GetEvents

Type.GetEvent メソッド (String)

指定したパブリック イベントを表す EventInfo オブジェクトを返します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function GetEvent ( _
    name As String _
) As EventInfo
Dim instance As Type
Dim name As String
Dim returnValue As EventInfo

returnValue = instance.GetEvent(name)
public EventInfo GetEvent (
    string name
)
public:
virtual EventInfo^ GetEvent (
    String^ name
) sealed
public final EventInfo GetEvent (
    String name
)
public final function GetEvent (
    name : String
) : EventInfo

パラメータ

name

現在の Type で宣言または継承されているイベントの名前を格納する String。

戻り値
現在の Type で宣言または継承されている指定パブリック イベントが存在する場合は、そのイベントを表す EventInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentNullException

name が null 参照 (Visual Basic では Nothing) です。

解説解説

name の検索では大文字と小文字が区別されます。検索には、public static および public のインスタンス イベントが含まれます。

要求された型がパブリックではなく、呼び出し元に現在のアセンブリ外の非パブリック オブジェクトをリフレクションするための ReflectionPermission がない場合、このメソッドは null 参照 (Visual Basic では Nothing) を返します。

型に対するリフレクション時に Get メソッドによって返される基本クラスのメンバを次の表に示します。

  1. 名前と署名による隠ぺいでは、カスタム修飾子、戻り値の型、パラメータの型、sentinel、およびアンマネージ呼び出し規約を含めて、署名のすべての部分が判断の対象となります。これはバイナリ比較です。

  2. リフレクションの場合、プロパティおよびイベントは名前と署名によって隠ぺいされています。基本クラスに get アクセサと set アクセサの両方を持つプロパティがあり、派生クラスには get アクセサしかない場合、派生クラスのプロパティにより基本クラスのプロパティが隠ぺいされ、基本クラスの set アクセサにはアクセスできません。

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

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

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

使用例使用例

EventInfo オブジェクトを作成し、指定したイベントのボタン クラスのイベントを取得する例を次に示します。

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
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);
        }
    }
}
#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
 );
   }
}
import System.*;
import System.Reflection.*;
import System.Security.*;

class MyEventExample
{
    public static void main(String[]
 args)
    {
        try {
            Type myType = System.Windows.Forms.Button.class.ToType();
            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.get_Message());
        }
        catch (ArgumentNullException e) {
            Console.WriteLine("An exception occurred.");
            Console.WriteLine("Message :" + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("The following exception was raised : {0}"
,
                e.get_Message());
        }
    } //main
} //MyEventExample
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

_Type.GetEvent メソッド




英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Type.GetEvent」の関連用語

Type.GetEventのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Type.GetEventのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS