Type.GetEvents 메서드

정의

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

오버로드

GetEvents()

현재 Type에 의해 선언되거나 상속되는 모든 public 이벤트를 반환합니다.

GetEvents(BindingFlags)

파생 클래스에서 재정의되면, 현재 Type에 의해 선언되거나 상속되는 이벤트를 지정된 바인딩 제약 조건으로 검색합니다.

GetEvents()

현재 Type에 의해 선언되거나 상속되는 모든 public 이벤트를 반환합니다.

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()

반환

현재 EventInfo에 의해 선언되거나 상속되는 모든 public 이벤트를 나타내는 Type 개체의 배열입니다.

또는

현재 EventInfo에 public 이벤트가 없을 경우 Type 형식의 빈 배열입니다.

구현

예제

다음 예제에서는 개체의 EventInfo 배열을 가져오고 클래스에 대한 Button 모든 이벤트를 가져오고 이벤트 이름을 표시합니다. Visual Basic 예제를 컴파일하려면 다음 명령줄을 사용합니다.

vbc type_getevents1.vb /r:System.Windows.Forms.dll /r:System.dll

#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;
      array<EventInfo^>^myEvents = myType->GetEvents();
      Console::WriteLine( "The events on the Button class are: " );
      for ( int index = 0; index < myEvents->Length; index++ )
      {
         Console::WriteLine( myEvents[ index ] );

      }
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
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

설명

이벤트가 퍼블릭인 메서드 또는 접근자가 하나 이상 있는 경우 이벤트를 리플렉션할 공용으로 간주됩니다. 그렇지 않으면 이벤트는 프라이빗 이벤트로 간주되며 이를 얻으려면 (Visual Basic에서는 을 사용하여 값을 결합)을 사용해야 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static 합니다.Or

.NET 6 및 이전 버전 GetEvents 에서 메서드는 사전순 또는 선언 순서와 같은 특정 순서로 이벤트를 반환하지 않습니다. 코드는 해당 순서가 다르기 때문에 이벤트가 반환되는 순서에 따라 달라지지 않아야 합니다. 그러나 .NET 7부터는 어셈블리의 메타데이터 순서에 따라 순서가 결정적입니다.

이 메서드는 파생 클래스로 재정의할 수 있습니다.

다음 표에서는 형식을 반영할 때 메서드에서 Get 반환되는 기본 클래스의 멤버를 보여 줍니다.

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

  2. 리플렉션의 경우 속성 및 이벤트는 이름별 숨기기 및 서명입니다. 기본 클래스에 get 및 set 접근자가 모두 있는 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기며 기본 클래스의 setter에 액세스할 수 없습니다.

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

현재 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()

매개 변수

bindingAttr
BindingFlags

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

또는

빈 배열을 반환하는 Default입니다.

반환

현재 EventInfo에 의해 선언되거나 상속되는 이벤트 중 지정된 바인딩 제약 조건과 일치하는 모든 이벤트를 나타내는 Type 개체의 배열입니다.

또는

현재 EventInfo에 이벤트가 없거나 해당 바인딩 제약 조건과 일치하는 이벤트가 없을 경우 Type 형식의 빈 배열입니다.

구현

예제

다음 예제에서는 지정된 바인딩 플래그와 일치하는 개체의 EventInfo 배열을 가져오고, 클래스에 대한 Button 모든 이벤트를 가져오고, 이벤트 이름을 표시합니다. Visual Basic 예제를 컴파일하려면 다음 명령줄을 사용합니다.

vbc type_getevents2.vb /r:System.Windows.Forms.dll /r:System.dll

#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
   {
      
      // Create a bitmask based on BindingFlags.
      BindingFlags myBindingFlags = static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public);
      Type^ myTypeEvent = System::Windows::Forms::Button::typeid;
      array<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 ] );

      }
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
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.Static 를 지정 BindingFlags.Instance 해야 합니다.

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

  • 비공용 이벤트(즉, 프라이빗, 내부 및 보호된 이벤트)를 검색에 포함하도록 지정 BindingFlags.NonPublic 합니다. 기본 클래스의 보호된 이벤트와 내부 이벤트만 반환됩니다. 기본 클래스의 private 이벤트는 반환되지 않습니다.

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

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

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

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

이벤트가 퍼블릭인 메서드 또는 접근자가 하나 이상 있는 경우 이벤트를 리플렉션할 공용으로 간주됩니다. 그렇지 않으면 이벤트는 프라이빗 이벤트로 간주되며 이를 얻으려면 (Visual Basic에서는 을 사용하여 값을 결합)을 사용해야 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static 합니다.Or

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

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

추가 정보

적용 대상