次の方法で共有


Type.GetEvent メソッド (String)

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

Overloads Public Function GetEvent( _
   ByVal name As String _) As EventInfo
[C#]
public EventInfo GetEvent(stringname);
[C++]
public: EventInfo* GetEvent(String* name);
[JScript]
public function GetEvent(
   name : String) : EventInfo;

パラメータ

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

戻り値

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

例外

例外の種類 条件
ArgumentNullException name が null 参照 (Visual Basic では Nothing) です。

解説

name の検索では大文字と小文字が区別されます。

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

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

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

使用例

[Visual Basic, C#, C++] 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

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

[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*  myEvent = myType->GetEvent(S"Click");
      if (myEvent != 0) {
         Console::WriteLine(S"Looking for the Click event in the Button class.");
         Console::WriteLine(myEvent);
      } else
         Console::WriteLine(S"The Click event is not available in the Button class.");
   } catch (SecurityException* e) {
      Console::WriteLine(S"An exception occurred.");
      Console::WriteLine(S"Message : {0}", e->Message);
   } catch (ArgumentNullException* e) {
      Console::WriteLine(S"An exception occurred.");
      Console::WriteLine(S"Message : {0}", e->Message);
   } catch (Exception* e) {
      Console::WriteLine(S"The following exception was raised : {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

.NET Framework セキュリティ:

参照

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