EventInfo 类

发现事件的属性 (Attribute) 并提供对事件元数据的访问权。

**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public MustInherit Class EventInfo
    Inherits MemberInfo
    Implements _EventInfo
用法
Dim instance As EventInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public abstract class EventInfo : MemberInfo, _EventInfo
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
public ref class EventInfo abstract : public MemberInfo, _EventInfo
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public abstract class EventInfo extends MemberInfo implements _EventInfo
SerializableAttribute 
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
public abstract class EventInfo extends MemberInfo implements _EventInfo

备注

事件与委托一起使用。事件侦听器实例化事件处理程序委托,每当事件源引发事件时会调用该事件处理程序委托。为了连接到事件源,事件侦听器将此委托添加到源上的调用列表中。引发事件时,将调用事件处理程序委托的调用方法。既支持多路广播事件通知,也支持单路广播事件通知。必须在元数据中标记 AddRemove 方法,以及与事件关联的事件处理程序委托类。

委托是面向对象的函数指针。在 C 或 C++ 中,函数指针是对方法的引用。与 C 或 C++ 函数指针相比,委托包含两个引用:对方法的引用和对支持该方法的对象的引用。委托可以调用方法而无需知道声明或继承该方法的类类型。委托只需要知道方法的返回类型和参数列表。

事件模型对单路广播委托和多路广播委托同样适用。当调用委托的调用方法时,只在单个对象上发生方法调用。多路广播修饰符可应用于委托声明,后者允许在调用委托的调用方法时调用多个方法。

GetCustomAttributes 的 inherit 参数为 true 时,对 EventInfo 调用 ICustomAttributeProvider.GetCustomAttributes 不遍历类型层次结构。使用 System.Attribute 继承自定义属性 (Attribute)。

给继承者的说明 当从 EventInfo 继承时,必须重写下列成员:GetAddMethodGetRemoveMethodGetRaiseMethod

示例

Imports System
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

' 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 Not (myEventBindingFlags Is 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 'Main
End Class 'MyEventExample
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);
        }
    }
}
#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 );
   }
}
import System.*;
import System.Reflection.*;
import System.Security.*;

class MyEventExample
{
    public static void main(String[] args)
    {
        try {
            // Creates a bitmask based on BindingFlags.
            BindingFlags myBindingFlags = BindingFlags.Instance
                | BindingFlags.Public | BindingFlags.NonPublic;
            Type myTypeBindingFlags = System.Windows.Forms.Button.class.ToType();
            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.get_Message());
        }
        catch (ArgumentNullException e) {
            Console.WriteLine("An exception occurred.");
            Console.WriteLine("Message :" + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("The following exception was raised : {0}",
                e.get_Message());
        }
    } //main
} //MyEventExample

继承层次结构

System.Object
   System.Reflection.MemberInfo
    System.Reflection.EventInfo

线程安全

该类型对于多线程操作是安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

EventInfo 成员
System.Reflection 命名空间