Type.GetEvents Type.GetEvents Type.GetEvents Type.GetEvents Method

定义

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

重载

GetEvents() GetEvents() GetEvents() GetEvents()

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

GetEvents(BindingFlags) GetEvents(BindingFlags) GetEvents(BindingFlags) 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() GetEvents() GetEvents() 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
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

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

注解

如果它具有至少一个方法或访问器,它是公共的事件被视为公共反射。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
PropertyProperty 不适用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. 按名称和签名隐藏会考虑所有的签名,包括自定义修饰符,部分返回类型、 参数类型、 标记和非托管调用约定。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) GetEvents(BindingFlags) GetEvents(BindingFlags) 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 BindingFlags BindingFlags 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
Imports System.Reflection
Imports System.Security
Imports System.Windows.Forms
Imports Microsoft.VisualBasic

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

注解

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.Static为了获取返回。You 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.

另请参阅

适用于