次の方法で共有


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

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

Overloads Public MustOverride Function GetEvent( _
   ByVal name As String, _   ByVal bindingAttr As BindingFlags _) As EventInfo
[C#]
public abstract EventInfo GetEvent(stringname,BindingFlagsbindingAttr);
[C++]
public: virtual EventInfo* GetEvent(String* name,BindingFlagsbindingAttr) = 0;
[JScript]
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.Instance または BindingFlags.Static のいずれかを指定する必要があります。
  • 検索対象にパブリック イベントを含めるための BindingFlags.Public を指定します。
  • 検索対象にパブリックではないイベント (つまり、プライベート イベントやプロテクト イベント) を含めるための BindingFlags.NonPublic を指定します。
  • 階層構造の上位にある静的メンバを含めるための BindingFlags.FlattenHierarchy を指定します。

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

  • name の大文字と小文字の違いを無視する場合は BindingFlags.IgnoreCase
  • Type で宣言されたイベントだけを検索するための BindingFlags.DeclaredOnly 。単に継承されただけのイベントは検索されません。

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

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

使用例

 
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

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

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.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 = __typeof(System::Windows::Forms::Button);
      EventInfo*  myEventBindingFlags = myTypeBindingFlags->GetEvent(S"Click", myBindingFlags);
      if (myEventBindingFlags != 0) {
         Console::WriteLine(S"Looking for the Click event in the Button class with the specified BindingFlags.");
         Console::WriteLine(myEventBindingFlags);
      } else
         Console::WriteLine(S"The Click event is not available with 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 | BindingFlags | DefaultBinder | ReflectionPermission | GetEvents