次の方法で共有


Type.GetEvents メソッド ()

現在の Type で宣言または継承されているすべてのパブリック イベントを返します。

Overloads Public Overridable Function GetEvents() As EventInfo()
[C#]
public virtual EventInfo[] GetEvents();
[C++]
public: virtual EventInfo* GetEvents() [];
[JScript]
public function GetEvents() : EventInfo[];

戻り値

現在の Type で宣言または継承されているすべてのパブリック イベントを表す EventInfo オブジェクトの配列。

または

現在の Type にパブリック イベントがない場合は、 EventInfo 型の空の配列。

解説

このメソッドは、派生クラスでオーバーライドできます。

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

メンバ型 静的 非静的
コンストラクタ いいえ いいえ
フィールド いいえ はい。フィールドは常に名前と署名によって隠ぺいされます。
イベント 適用なし 共通型システムの規則では、継承は、プロパティを実装するメソッドの継承と同じになります。リフレクションは、プロパティを名前と署名によって隠ぺいされているとして扱います。下記のメモ 2 を参照してください。
メソッド いいえ はい。メソッド (仮想メソッドと非仮想メソッドの両方) は、名前によって隠蔽することもできますし、名前と署名によって隠蔽することもできます。
入れ子になった型 いいえ いいえ
プロパティ 適用なし 共通型システムの規則では、継承は、プロパティを実装するメソッドの継承と同じになります。リフレクションは、プロパティを名前と署名によって隠ぺいされているとして扱います。下記のメモ 2 を参照してください。
  1. 名前と署名による隠ぺいでは、カスタム修飾子、戻り値の型、パラメータの型、sentinel、およびアンマネージ呼び出し規約を含めて、署名のすべての部分が判断の対象となります。これはバイナリ比較です。
  2. リフレクションの場合、プロパティおよびイベントは名前と署名によって隠ぺいされています。基本クラスに get アクセサと set アクセサの両方を持つプロパティがあり、派生クラスには get アクセサしかない場合、派生クラスのプロパティにより基本クラスのプロパティが隠ぺいされ、基本クラスの set アクセサにはアクセスできません。
  3. カスタム属性は、共通の型システムの一部ではありません。

使用例

[Visual Basic, C#, C++] EventInfo オブジェクトの配列を取得し、 Button クラスのイベントをすべて取得して、イベント名を表示する例を次に示します。Visual Basic の例をコンパイルするには、次のコマンド ラインを使用します。

[Visual Basic, C#, C++] vbc type_getevents1.vb /r:System.Windows.Forms.dll /r:System.dll

 
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

[C#] 
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);
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Security;

int main()
{ 
   try {
      Type* myType = __typeof(System::Windows::Forms::Button);
      EventInfo* myEvents[] = myType->GetEvents();
      Console::WriteLine(S"The events on the Button class are: ");
      for (int index = 0; index < myEvents->Length; index++) {
         Console::WriteLine(myEvents[index]);
      }
   } catch (SecurityException* e) {
      Console::WriteLine(S"SecurityException: {0}", e->Message);
   } catch (ArgumentNullException* e) {
      Console::WriteLine(S"ArgumentNullException: {0}", e->Message);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception: {0}", e->Message);
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Type クラス | Type メンバ | System 名前空間 | Type.GetEvents オーバーロードの一覧 | EventInfo | GetEvent