EventInfo.EventHandlerType 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 Type 與此事件相關聯的底層事件處理代理的物件。
public:
virtual property Type ^ EventHandlerType { Type ^ get(); };
public:
property Type ^ EventHandlerType { Type ^ get(); };
public virtual Type EventHandlerType { get; }
public virtual Type? EventHandlerType { get; }
public Type EventHandlerType { get; }
member this.EventHandlerType : Type
Public Overridable ReadOnly Property EventHandlerType As Type
Public ReadOnly Property EventHandlerType As Type
屬性值
一個只讀 Type 的物件,代表代理事件處理器。
實作
例外狀況
來電者沒有所需的權限。
範例
以下範例利用該 EventHandlerType 屬性來發現事件的代理類型並顯示其參數類型。
範例定義了一個名為 MyDelegate 的代理,以及一個名為 evMyDelegate的事件。 方法中的 Main 程式碼透過取得事件的代理類型、 Invoke 代理類型的方法,然後取得參數來發現事件簽名。
// The following example uses instances of classes in
// the System.Reflection namespace to discover an event argument type.
using System;
using System.Reflection;
public delegate void MyDelegate(int i);
public class MainClass
{
public event MyDelegate ev;
public static void Main()
{
Type delegateType = typeof(MainClass).GetEvent("ev").EventHandlerType;
MethodInfo invoke = delegateType.GetMethod("Invoke");
ParameterInfo[] pars = invoke.GetParameters();
foreach (ParameterInfo p in pars)
{
Console.WriteLine(p.ParameterType);
}
}
}
// The example displays the following output:
// System.Int32
Imports System.Reflection
Public Delegate Sub MyDelegate(ByVal i As Integer)
Public Class MainClass
Public Event ev As MyDelegate
Public Shared Sub Main()
Dim delegateType As Type = GetType(MainClass).GetEvent("ev").EventHandlerType
Dim invoke As MethodInfo = delegateType.GetMethod("Invoke")
Dim pars As ParameterInfo() = invoke.GetParameters()
For Each p As ParameterInfo In pars
Console.WriteLine(p.ParameterType)
Next
End Sub
End Class
' The example displays the following output:
' System.Int32