Type.GetEvent 메서드

정의

현재 Type에 의해 선언되거나 상속되는 특정 이벤트를 가져옵니다.

오버로드

GetEvent(String, BindingFlags)

파생 클래스에서 재정의되면, 지정된 바인딩 제약 조건을 사용하여 지정된 이벤트를 나타내는 EventInfo 개체를 반환합니다.

GetEvent(String)

지정된 public 이벤트를 나타내는 EventInfo 개체를 반환합니다.

GetEvent(String, BindingFlags)

파생 클래스에서 재정의되면, 지정된 바인딩 제약 조건을 사용하여 지정된 이벤트를 나타내는 EventInfo 개체를 반환합니다.

public:
 abstract System::Reflection::EventInfo ^ GetEvent(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo? GetEvent (string name, System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo GetEvent (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetEvent : string * System.Reflection.BindingFlags -> System.Reflection.EventInfo
Public MustOverride Function GetEvent (name As String, bindingAttr As BindingFlags) As EventInfo

매개 변수

name
String

현재 Type에 의해 선언되거나 상속되는 이벤트의 이름이 포함된 문자열입니다.

bindingAttr
BindingFlags

검색 방법을 지정하는 열거형 값의 비트 조합입니다.

또는 null을 반환하는 Default입니다.

반환

EventInfo

현재 Type에 의해 선언되거나 상속되는, 지정한 이벤트(있는 경우)를 나타내는 개체이고, 그렇지 않으면 null입니다.

구현

예외

name이(가) null인 경우

예제

다음 코드 예제에서는 메서드를 사용 하 여 GetEvent(String, BindingFlags) static (Visual Basic)이 아닌 "클릭" 이라는 public 또는 public이 아닌 이벤트의 형식을 검색 합니다 Shared .

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

' 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 myEventBindingFlags IsNot 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
End Class

설명

다음 BindingFlags 필터 플래그를 사용 하 여 검색에 포함할 이벤트를 정의할 수 있습니다.

  • BindingFlags.Instance반환을 얻으려면 또는 중 하나를 지정 해야 합니다 BindingFlags.Static .

  • BindingFlags.Public검색에 공용 이벤트를 포함 하도록 지정 합니다.

  • BindingFlags.NonPublic검색에 public이 아닌 이벤트 (즉, private, internal 및 protected 이벤트)를 포함 하도록 지정 합니다.

  • BindingFlags.FlattenHierarchy public 계층의 정적 멤버를 포함 하도록 지정 합니다. 상속 된 protected private 클래스의 정적 멤버는 포함 되지 않습니다.

다음 BindingFlags 한정자 플래그를 사용 하 여 검색의 작동 방식을 변경할 수 있습니다.

  • BindingFlags.IgnoreCase 의 대/소문자를 무시 하려면입니다 name .

  • BindingFlags.DeclaredOnly 단순히 상속 된 이벤트가 아니라에 선언 된 이벤트만 검색 합니다 Type .

자세한 내용은 System.Reflection.BindingFlags를 참조하세요.

이벤트는 public 인 메서드나 접근자가 하나 이상 있는 경우 리플렉션에 public으로 간주 됩니다. 그렇지 않으면 이벤트는 전용으로 간주 되며, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basic에서 값을 결합 하 여)를 사용 해야 합니다 Or .

현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 EventInfo 형식 매개 변수를 적절 한 형식 인수로 대체 한를 반환 합니다.

현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 이벤트를 검색 합니다.

추가 정보

적용 대상

GetEvent(String)

지정된 public 이벤트를 나타내는 EventInfo 개체를 반환합니다.

public:
 System::Reflection::EventInfo ^ GetEvent(System::String ^ name);
public:
 virtual System::Reflection::EventInfo ^ GetEvent(System::String ^ name);
public System.Reflection.EventInfo? GetEvent (string name);
public System.Reflection.EventInfo GetEvent (string name);
member this.GetEvent : string -> System.Reflection.EventInfo
abstract member GetEvent : string -> System.Reflection.EventInfo
override this.GetEvent : string -> System.Reflection.EventInfo
Public Function GetEvent (name As String) As EventInfo

매개 변수

name
String

현재 Type에 의해 선언되거나 상속되는 이벤트의 이름이 포함된 문자열입니다.

반환

EventInfo

현재 Type에 의해 선언되거나 상속되는, 지정한 public 이벤트(있는 경우)를 나타내는 개체이고, 그렇지 않으면 null입니다.

구현

예외

name이(가) null인 경우

예제

다음 예제에서는 개체를 만들고 EventInfo 지정 된 이벤트에 대 한 단추 클래스의 이벤트를 가져옵니다.

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

' 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
End Class

설명

이벤트는 public 인 메서드나 접근자가 하나 이상 있는 경우 리플렉션에 public으로 간주 됩니다. 그렇지 않으면 이벤트는 전용으로 간주 되며, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basic에서 값을 결합 하 여)를 사용 해야 합니다 Or .

검색은 대 name /소문자를 구분 합니다. 검색에는 public static 및 public instance 이벤트가 포함 됩니다.

다음 표에서는 Get 형식에 대해 리플렉션할 때 메서드에서 반환 되는 기본 클래스의 멤버를 보여 줍니다.

멤버 형식 정적 비정적
생성자 아니요 아니요
필드 아니요 예. 필드는 항상 이름 및 시그니처로 숨겨집니다.
이벤트 해당 없음 공용 형식 시스템 규칙은 상속이 속성을 구현 하는 메서드와 동일 하다는 것입니다. 리플렉션에서는 속성을 이름으로 숨기기-서명으로 처리 합니다. 아래의 참고 2를 참조 하세요.
메서드 아니요 예. 메서드 (가상 및 비가상)는 이름을 기준으로 숨기 거 나 이름에 따라 숨길 수 있습니다.
중첩 형식 아니요 아니요
속성 해당 없음 공용 형식 시스템 규칙은 상속이 속성을 구현 하는 메서드와 동일 하다는 것입니다. 리플렉션에서는 속성을 이름으로 숨기기-서명으로 처리 합니다. 아래의 참고 2를 참조 하세요.
  1. 이름 및 시그니처는 사용자 지정 한정자, 반환 형식, 매개 변수 형식, 센티널 및 관리 되지 않는 호출 규칙을 포함 하 여 시그니처의 모든 부분을 고려 합니다. 이는 이진 비교입니다.

  2. 리플렉션의 경우 속성과 이벤트는 이름 및 시그니처로 숨겨집니다. 기본 클래스에 get 및 set 접근자가 모두 포함 된 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기고 기본 클래스의 setter에 액세스할 수 없게 됩니다.

  3. 사용자 지정 특성은 공용 형식 시스템의 일부가 아닙니다.

현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 EventInfo 형식 매개 변수를 적절 한 형식 인수로 대체 한를 반환 합니다.

현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 이벤트를 검색 합니다.

추가 정보

적용 대상