Type.GetEvents 方法

定义

获取由当前 Type 声明或继承的事件。Gets the events that are declared or inherited by the current Type.

重载

GetEvents()

返回由当前 Type 声明或继承的所有公共事件。Returns all the public events that are declared or inherited by the current Type.

GetEvents(BindingFlags)

当在派生类中重写时,使用指定绑定约束,搜索由当前 Type 声明或继承的事件。When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints.

GetEvents()

返回由当前 Type 声明或继承的所有公共事件。Returns all the public events that are declared or inherited by the current Type.

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 声明或继承的所有公共事件的 Type 对象数组。An array of EventInfo objects representing all the public events which are declared or inherited by the current Type.

-or- 如果当前 EventInfo 没有默认成员,则为 Type 类型的空数组。An empty array of type EventInfo, if the current Type does not have public events.

实现

示例

下面的示例获取 EventInfo 对象的数组,获取 Button 类的所有事件,并显示事件名称。The following example obtains an array of EventInfo objects, gets all the events for a Button class, and displays the event names. 若要编译 Visual Basic 示例,请使用以下命令行:To compile the Visual Basic example, use the following command line:

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);
        }
    }
}
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

注解

如果某个事件至少有一个公共方法或访问器,则该事件将被视为 "公共"。An event is considered public to reflection if it has at least one method or accessor that is public. 否则,该事件将被视为私有事件,并且你|必须|使用 BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static (在 Visual Basic 中,使用 Or组合值)来获取它。Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

GetEvents 方法不按特定顺序返回事件,如字母顺序或声明顺序。The GetEvents method does not return events in a particular order, such as alphabetical or declaration order. 你的代码不能依赖于事件的返回顺序,因为该顺序会有所不同。Your code must not depend on the order in which events are returned, because that order varies.

此方法可由派生类重写。This method can be overridden by a derived class.

下表显示了在类型上反射时,Get 方法返回基类的成员。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成员类型Member Type StaticStatic 非静态Non-Static
构造函数Constructor NoNo NoNo
字段Field NoNo 可以。Yes. 字段始终按名称和签名隐藏。A field is always hide-by-name-and-signature.
事件Event 不适用Not applicable 通用类型系统规则是指继承与实现属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为隐藏的名称和签名。Reflection treats properties as hide-by-name-and-signature. 请参阅下面的注释2。See note 2 below.
方法Method NoNo 可以。Yes. 方法(虚拟和非虚拟)可按名称隐藏或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套类型Nested Type NoNo NoNo
属性Property 不适用Not applicable 通用类型系统规则是指继承与实现属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为隐藏的名称和签名。Reflection treats properties as hide-by-name-and-signature. 请参阅下面的注释2。See note 2 below.
  1. 按名称和签名隐藏将考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、个 sentinel 和非托管调用约定。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 这是二进制比较。This is a binary comparison.

  2. 对于反射,属性和事件是按名称和签名隐藏的。For reflection, properties and events are hide-by-name-and-signature. 如果在基类中同时具有 get 访问器和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且你将无法访问基类的资源库。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自定义属性不属于通用类型系统。Custom attributes are not part of the common type system.

如果当前 Type 表示构造泛型类型,则此方法返回 EventInfo 对象,这些对象的类型参数被替换为相应的类型参数。If the current Type represents a constructed generic type, this method returns the EventInfo objects with the type parameters replaced by the appropriate type arguments.

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法会搜索类约束的事件。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the events of the class constraint.

另请参阅

GetEvents(BindingFlags)

当在派生类中重写时,使用指定绑定约束,搜索由当前 Type 声明或继承的事件。When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints.

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

枚举值的按位组合,这些值指定如何进行搜索。A bitwise combination of the enumeration values that specify how the search is conducted.

-or-

若为 Default,则返回空数组。Default to return an empty array.

返回

EventInfo 对象的数组,表示当前 Type 所声明或继承的与指定绑定约束匹配的所有事件。An array of EventInfo objects representing all events that are declared or inherited by the current Type that match the specified binding constraints.

-or- 如果当前 EventInfo 没有事件,或者如果没有一个事件匹配绑定约束,则为 Type 类型的空数组。An empty array of type EventInfo, if the current Type does not have events, or if none of the events match the binding constraints.

实现

示例

下面的示例获取与指定绑定标志匹配的 EventInfo 对象的数组,获取 Button 类的所有事件,并显示事件名称。The following example obtains an array of EventInfo objects that match the specified binding flags, gets all the events for a Button class, and displays the event names. 若要编译 Visual Basic 示例,请使用以下命令行:To compile the Visual Basic example, use the following command line:

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);
        }
    }
}
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

注解

GetEvents 方法不按特定顺序返回事件,如字母顺序或声明顺序。The GetEvents method does not return events in a particular order, such as alphabetical or declaration order. 你的代码不能依赖于事件的返回顺序,因为该顺序会有所不同。Your code must not depend on the order in which events are returned, because that order varies.

以下 BindingFlags 筛选器标志可用于定义要包括在搜索中的事件:The following BindingFlags filter flags can be used to define which events to include in the search:

  • 若要获取返回,必须指定 BindingFlags.InstanceBindingFlags.StaticYou must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定 BindingFlags.Public 以在搜索中包括公共事件。Specify BindingFlags.Public to include public events in the search.

  • 指定 BindingFlags.NonPublic 以在搜索中包括非公共事件(即,私有事件、内部事件和受保护事件)。Specify BindingFlags.NonPublic to include non-public events (that is, private, internal, and protected events) in the search. 仅返回基类的受保护事件和内部事件;不返回基类上的私有事件。Only protected and internal events on base classes are returned; private events on base classes are not returned.

  • 指定 BindingFlags.FlattenHierarchy 包括层次结构中的 publicprotected 静态成员;不包括继承类中 private 静态成员。Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

以下 BindingFlags 修饰符标志可用于更改搜索的工作方式:The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly 仅搜索 Type上声明的事件,而不搜索仅继承的事件。BindingFlags.DeclaredOnly to search only the events declared on the Type, not events that were simply inherited.

有关更多信息,请参见System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

如果某个事件至少有一个公共方法或访问器,则该事件将被视为 "公共"。An event is considered public to reflection if it has at least one method or accessor that is public. 否则,该事件将被视为私有事件,并且你|必须|使用 BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static (在 Visual Basic 中,使用 Or组合值)来获取它。Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

如果当前 Type 表示构造泛型类型,则此方法返回 EventInfo 对象,这些对象的类型参数被替换为相应的类型参数。If the current Type represents a constructed generic type, this method returns the EventInfo objects with the type parameters replaced by the appropriate type arguments.

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法会搜索类约束的事件。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the events of the class constraint.

另请参阅

适用于