语言

Type.GetEvents 方法

定义

获取由当前 Type声明或继承的事件。

重载

名称 说明
GetEvents()

返回当前 Type声明或继承的所有公共事件。

GetEvents(BindingFlags)

在派生类中重写时,使用指定的绑定约束搜索由当前 Type声明或继承的事件。

GetEvents()

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

返回当前 Type声明或继承的所有公共事件。

public:
 virtual cli::array <System::Reflection::EventInfo ^> ^ GetEvents();
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)]
public virtual System.Reflection.EventInfo[] GetEvents();
public virtual System.Reflection.EventInfo[] GetEvents();
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)>]
abstract member GetEvents : unit -> System.Reflection.EventInfo[]
override this.GetEvents : unit -> System.Reflection.EventInfo[]
abstract member GetEvents : unit -> System.Reflection.EventInfo[]
override this.GetEvents : unit -> System.Reflection.EventInfo[]
Public Overridable Function GetEvents () As EventInfo()

返回

表示当前EventInfo声明或继承的所有公共事件的对象数组Type。

-或-

如果当前EventInfo没有公共事件,则为空数组。Type

实现

特性

示例

以下示例获取对象数组 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

注解

如果事件至少有一个公共方法或访问器是公共的,则被视为公共事件。 否则,该事件被视为私有事件,并且必须使用 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static(在 Visual Basic 中,使用 Or) 组合这些值来获取它。

在 .NET 6 及更早版本中,GetEvents 方法不会按特定顺序(如字母顺序或声明顺序)返回事件。 代码不得依赖于返回事件的顺序,因为该顺序会有所不同。 但是,从 .NET 7 开始,排序是基于程序集中的元数据排序确定性的。

此方法可由派生类重写。

下表显示了通过 Get 方法在反射类型时返回的基类成员。

成员类型 Static 非静态
构造函数 否 否
领域 否 Yes. 字段始终是按名称和签名隐藏的。
事件 不適用 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。
方法 否 Yes. 方法(虚拟和非虚拟)可以按名称隐藏或按名称和签名隐藏。
嵌套类型 否 否
财产 不適用 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。
  1. 按名称和签名隐藏会考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinel 和非托管调用约定。 这是二进制比较。

  2. 对于反射,属性和事件通过名称和签名进行隐藏。 如果基类中同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且无法访问基类上的 setter。

  3. 自定义属性不是常见类型系统的一部分。

如果当前 Type 表示构造的泛型类型,此方法将返回 EventInfo 具有由相应类型参数替换的类型参数的对象。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,此方法将搜索类约束的事件。

另请参阅

适用于

GetEvents(BindingFlags)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

在派生类中重写时,使用指定的绑定约束搜索由当前 Type声明或继承的事件。

public:
 abstract cli::array <System::Reflection::EventInfo ^> ^ GetEvents(System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicEvents | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)]
public abstract System.Reflection.EventInfo[] GetEvents(System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo[] GetEvents(System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicEvents | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)>]
abstract member GetEvents : System.Reflection.BindingFlags -> System.Reflection.EventInfo[]
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;
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。

如果事件至少有一个公共方法或访问器是公共的,则被视为公共事件。 否则,该事件被视为私有事件,并且必须使用 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static(在 Visual Basic 中,使用 Or) 组合这些值来获取它。

如果当前 Type 表示构造的泛型类型,此方法将返回 EventInfo 具有由相应类型参数替换的类型参数的对象。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,此方法将搜索类约束的事件。

另请参阅

适用于