Type.GetEvents メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の Typeによって宣言または継承されるイベントを取得します。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetEvents() |
現在の Typeによって宣言または継承されているすべてのパブリック イベントを返します。 |
| GetEvents(BindingFlags) |
派生クラスでオーバーライドされた場合は、指定したバインド制約を使用して、現在の Typeによって宣言または継承されるイベントを検索します。 |
GetEvents()
現在の Typeによって宣言または継承されているすべてのパブリック イベントを返します。
public:
virtual cli::array <System::Reflection::EventInfo ^> ^ GetEvents();
public virtual System.Reflection.EventInfo[] GetEvents();
abstract member GetEvents : unit -> System.Reflection.EventInfo[]
override this.GetEvents : unit -> System.Reflection.EventInfo[]
Public Overridable Function GetEvents () As EventInfo()
返品
現在のTypeによって宣言または継承されているすべてのパブリック イベントを表すEventInfo オブジェクトの配列。
-または-
現在のTypeにパブリック イベントがない場合は、EventInfo型の空の配列。
実装
例
次の例では、 EventInfo オブジェクトの配列を取得し、 Button クラスのすべてのイベントを取得し、イベント名を表示します。 Visual Basic例をコンパイルするには、次のコマンド ラインを使用します。
vbc type_getevents1.vb /r:System.Windows.Forms.dll /r:System.dll
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);
}
}
}
open System
open System.Reflection
open System.Security
try
// Creates a bitmask based on BindingFlags.
let myBindingFlags = BindingFlags.Instance ||| BindingFlags.Public
let myTypeEvent = typeof<System.Windows.Forms.Button>
let myEventsBindingFlags = myTypeEvent.GetEvents myBindingFlags
printfn "\nThe events on the Button class with the specified BindingFlags are : "
for flag in myEventsBindingFlags do
printfn $"{flag}"
with
| :? SecurityException as e ->
printfn $"SecurityException: {e.Message}"
| :? ArgumentNullException as e ->
printfn $"ArgumentNullException: {e.Message}"
| e ->
printfn $"Exception : {e.Message}"
Imports System.Reflection
Imports System.Security
' 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
End Class
注釈
イベントは、少なくとも 1 つのメソッドまたはアクセサーがパブリックである場合、リフレクションに対してパブリックと見なされます。 それ以外の場合、イベントはプライベートと見なされ、BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basicでは、Or を使用して値を結合) を使用してイベントを取得する必要があります。
.NET 6 以前のバージョンでは、GetEvents メソッドは、アルファベット順や宣言順などの特定の順序でイベントを返しません。 コードは、イベントが返される順序に依存してはなりません。順序は異なるためです。 ただし、.NET 7 以降では、順序付けはアセンブリ内のメタデータの順序に基づいて決定論的です。
このメソッドは、派生クラスによってオーバーライドできます。
次の表は、型に反映するときに、 Get メソッドによって返される基底クラスのメンバーを示しています。
| メンバーの種類 | スタティック | 非静的 |
|---|---|---|
| コンストラクター | いいえ | いいえ |
| フィールド | いいえ | Yes. フィールドは常に名前と署名によって非表示になります。 |
| Event | 適用なし | 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。 |
| Method | いいえ | Yes. メソッド (仮想と非仮想の両方) は、名前で非表示にすることも、名前と署名で非表示にすることもできます。 |
| 入れ子にされた型 | いいえ | いいえ |
| 財産 | 適用なし | 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。 |
名前と署名による隠蔽は、カスタム修飾子、戻り値の型、パラメーター型、センチネル、そしてアンマネージド呼び出し規則を含む、署名のすべての部分を考慮します。 これはバイナリ比較です。
リフレクションの場合、プロパティとイベントは名前と署名によって非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしかない場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。
カスタム属性は、共通型システムの一部ではありません。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ EventInfo オブジェクトを返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のイベントを検索します。
こちらもご覧ください
適用対象
GetEvents(BindingFlags)
派生クラスでオーバーライドされた場合は、指定したバインド制約を使用して、現在の Typeによって宣言または継承されるイベントを検索します。
public:
abstract cli::array <System::Reflection::EventInfo ^> ^ GetEvents(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo[] GetEvents(System.Reflection.BindingFlags bindingAttr);
abstract member GetEvents : System.Reflection.BindingFlags -> System.Reflection.EventInfo[]
Public MustOverride Function GetEvents (bindingAttr As BindingFlags) As EventInfo()
パラメーター
返品
指定したバインド制約に一致する現在のTypeによって宣言または継承されるすべてのイベントを表すEventInfo オブジェクトの配列。
-または-
現在のTypeにイベントがない場合、またはバインディング制約に一致するイベントがない場合は、EventInfo型の空の配列。
実装
例
次の例では、指定したバインド フラグに一致する EventInfo オブジェクトの配列を取得し、 Button クラスのすべてのイベントを取得し、イベント名を表示します。 Visual Basic例をコンパイルするには、次のコマンド ラインを使用します。
vbc type_getevents2.vb /r:System.Windows.Forms.dll /r:System.dll
using System;
using System.Reflection;
using System.Security;
class EventsSample
{
public static void Main()
{
try
{
// Create 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);
}
}
}
open System
open System.Reflection
open System.Security
try
// Creates a bitmask based on BindingFlags.
let myBindingFlags = BindingFlags.Instance ||| BindingFlags.Public
let myTypeEvent = typeof<System.Windows.Forms.Button>
let myEventsBindingFlags = myTypeEvent.GetEvents myBindingFlags
printfn "\nThe events on the Button class with the specified BindingFlags are : "
for flag in myEventsBindingFlags do
printfn $"{flag}"
with
| :? SecurityException as e ->
printfn $"SecurityException: {e.Message}"
| :? ArgumentNullException as e ->
printfn $"ArgumentNullException: {e.Message}"
| e ->
printfn $"Exception : {e.Message}"
Imports System.Reflection
Imports System.Security
Imports System.Windows.Forms
Class EventsSample
Public Shared Sub Main()
Try
' Create 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
End Class
注釈
.NET 6 以前のバージョンでは、GetEvents メソッドは、アルファベット順や宣言順などの特定の順序でイベントを返しません。 コードは、イベントが返される順序に依存してはなりません。順序は異なるためです。 ただし、.NET 7 以降では、順序付けはアセンブリ内のメタデータの順序に基づいて決定論的です。
次の BindingFlags フィルター フラグを使用して、検索に含めるイベントを定義できます。
戻り値を取得するには、
BindingFlags.InstanceまたはBindingFlags.Staticを指定する必要があります。検索にパブリック イベントを含める
BindingFlags.Publicを指定します。BindingFlags.NonPublicを指定して、非パブリック イベント (プライベート、内部、および保護されたイベント) を検索に含めます。 基底クラスの保護されたイベントと内部イベントのみが返されます。基底クラスのプライベート イベントは返されません。BindingFlags.FlattenHierarchyおよびpublic静的メンバーを階層に含めるprotectedを指定します。継承されたクラスprivate静的メンバーは含まれません。
次の BindingFlags 修飾子フラグを使用して、検索の動作を変更できます。
-
BindingFlags.DeclaredOnlyは、単に継承されたイベントではなく、 Typeで宣言されたイベントのみを検索します。
詳細については、System.Reflection.BindingFlags を参照してください。
イベントは、少なくとも 1 つのメソッドまたはアクセサーがパブリックである場合、リフレクションに対してパブリックと見なされます。 それ以外の場合、イベントはプライベートと見なされ、BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basicでは、Or を使用して値を結合) を使用してイベントを取得する必要があります。
現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターを持つ EventInfo オブジェクトを返します。
現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のイベントを検索します。