如何:使用反映連結委派
當您使用反映來載入和執行元件時,無法使用 C# +=
運算符或 Visual Basic AddHandler 語句 等語言功能來連結事件。 下列程序示範如何透過反映取得所有必要類型以將現有方法連結至事件,以及如何使用反映發出建立動態方法並將它連結至事件。
注意
如需連結事件處理委派的另一種方式,請參閱 EventInfo 類別之 AddEventHandler 方法的程式碼範例。
使用反映連結委派
載入包含引發事件之類型的組件。 通常會使用 Assembly.Load 方法載入組件。 為了簡化此範例,會使用目前組件中的衍生形式,因此使用 GetExecutingAssembly 方法來載入目前組件。
Assembly^ assem = Example::typeid->Assembly;
Assembly assem = typeof(Example).Assembly;
Dim assem As Assembly = GetType(Example).Assembly
取得代表類型的 Type 物件,並建立類型的執行個體。 因為表單具有無參數建構函式,所以在下列程式碼中使用 CreateInstance(Type) 方法。 如果您所建立的型別沒有無參數建構函式,則 CreateInstance 方法有數個其他多載可以使用。 新的執行個體會儲存為 Object 類型,以維護不知道組件的故事 (反映可讓您取得組件中的類型,而不需要事先知道其名稱)。
Type^ tExForm = assem->GetType("ExampleForm"); Object^ exFormAsObj = Activator::CreateInstance(tExForm);
Type tExForm = assem.GetType("ExampleForm"); Object exFormAsObj = Activator.CreateInstance(tExForm);
Dim tExForm As Type = assem.GetType("ExampleForm") Dim exFormAsObj As Object = _ Activator.CreateInstance(tExForm)
取得代表事件的 EventInfo 物件,並使用 EventHandlerType 屬性來取得用來處理事件之委派的類型。 在下列程式碼中,會取得 Click 事件的 EventInfo。
EventInfo^ evClick = tExForm->GetEvent("Click"); Type^ tDelegate = evClick->EventHandlerType;
EventInfo evClick = tExForm.GetEvent("Click"); Type tDelegate = evClick.EventHandlerType;
Dim evClick As EventInfo = tExForm.GetEvent("Click") Dim tDelegate As Type = evClick.EventHandlerType
取得代表處理事件之方法的 MethodInfo 物件。 本文稍後的 Example 區段中的完整程式代碼包含符合委派簽章的方法,該委派 EventHandler 會處理 Click 事件,但您也可以在運行時間產生動態方法。 如需詳細資訊,請參閱伴隨的程序中關於在執行階段使用動態方法產生事件處理常式的資訊。
MethodInfo^ miHandler = Type::GetType("Example")->GetMethod("LuckyHandler", BindingFlags::NonPublic | BindingFlags::Instance);
MethodInfo miHandler = typeof(Example).GetMethod("LuckyHandler", BindingFlags.NonPublic | BindingFlags.Instance);
Dim miHandler As MethodInfo = _ GetType(Example).GetMethod("LuckyHandler", _ BindingFlags.NonPublic Or BindingFlags.Instance)
使用 CreateDelegate 方法,建立委派的執行個體。 此方法是 static (在 Visual Basic 中為
Shared
),因此必須提供委派類型。 建議使用接受 MethodInfo 之 CreateDelegate 的多載。Delegate^ d = Delegate::CreateDelegate(tDelegate, this, miHandler);
Delegate d = Delegate.CreateDelegate(tDelegate, this, miHandler);
Dim d As [Delegate] = _ [Delegate].CreateDelegate(tDelegate, Me, miHandler)
取得
add
存取子方法,並叫用它來連結事件。 所有事件都有高階語言語法所隱藏的add
存取子和remove
存取子。 例如,C# 使用+=
運算子來連結事件,Visual Basic 則使用 AddHandler 陳述式。 下列程式碼會取得 Click 事件的add
存取子,以及透過晚期繫結叫用它,並傳入委派執行個體。 引數必須傳遞為陣列。MethodInfo^ addHandler = evClick->GetAddMethod(); array<Object^>^ addHandlerArgs = { d }; addHandler->Invoke(exFormAsObj, addHandlerArgs);
MethodInfo addHandler = evClick.GetAddMethod(); Object[] addHandlerArgs = { d }; addHandler.Invoke(exFormAsObj, addHandlerArgs);
Dim miAddHandler As MethodInfo = evClick.GetAddMethod() Dim addHandlerArgs() As Object = {d} miAddHandler.Invoke(exFormAsObj, addHandlerArgs)
測試事件。 下列程式碼顯示程式碼範例中所定義的表單。 按一下表單,會叫用事件處理常式。
Application::Run((Form^) exFormAsObj);
Application.Run((Form) exFormAsObj);
Application.Run(CType(exFormAsObj, Form))
使用動態方法在運行時間產生事件處理程式
使用輕量型動態方法和反映發出,可以在執行階段產生事件處理常式方法。 若要建構事件處理常式,您需要委派的傳回型別和參數類型。 這些可以透過檢查委派的
Invoke
方法取得。 下列程式碼使用GetDelegateReturnType
和GetDelegateParameterTypes
方法來取得這項資訊。 您可以在本文稍後的一節中找到這些方法的程序代碼。不需要命名 DynamicMethod,因此可以使用空字串。 在下列程式碼中,最後一個引數會建立動態方法與目前類型的關聯,並提供
Example
類別之所有公用和私用成員的委派存取權。Type^ returnType = GetDelegateReturnType(tDelegate); if (returnType != void::typeid) throw gcnew ApplicationException("Delegate has a return type."); DynamicMethod^ handler = gcnew DynamicMethod("", nullptr, GetDelegateParameterTypes(tDelegate), Example::typeid);
Type returnType = GetDelegateReturnType(tDelegate); if (returnType != typeof(void)) throw new ArgumentException("Delegate has a return type.", nameof(d)); DynamicMethod handler = new DynamicMethod("", null, GetDelegateParameterTypes(tDelegate), typeof(Example));
Dim returnType As Type = GetDelegateReturnType(tDelegate) If returnType IsNot GetType(Void) Then Throw New ArgumentException("Delegate has a return type.", NameOf(d)) End If Dim handler As New DynamicMethod( _ "", _ Nothing, _ GetDelegateParameterTypes(tDelegate), _ GetType(Example) _ )
產生方法主體。 這個方法會載入字串、呼叫接受字串之 MessageBox.Show 方法的多載、從堆疊取出傳回值 (因為處理常式沒有傳回型別) 並傳回。 若要深入了解如何發出動態方法,請參閱如何:定義和執行動態方法。
ILGenerator^ ilgen = handler->GetILGenerator(); array<Type^>^ showParameters = { String::typeid }; MethodInfo^ simpleShow = MessageBox::typeid->GetMethod("Show", showParameters); ilgen->Emit(OpCodes::Ldstr, "This event handler was constructed at run time."); ilgen->Emit(OpCodes::Call, simpleShow); ilgen->Emit(OpCodes::Pop); ilgen->Emit(OpCodes::Ret);
ILGenerator ilgen = handler.GetILGenerator(); Type[] showParameters = { typeof(String) }; MethodInfo simpleShow = typeof(MessageBox).GetMethod("Show", showParameters); ilgen.Emit(OpCodes.Ldstr, "This event handler was constructed at run time."); ilgen.Emit(OpCodes.Call, simpleShow); ilgen.Emit(OpCodes.Pop); ilgen.Emit(OpCodes.Ret);
Dim ilgen As ILGenerator = handler.GetILGenerator() Dim showParameters As Type() = {GetType(String)} Dim simpleShow As MethodInfo = _ GetType(MessageBox).GetMethod("Show", showParameters) ilgen.Emit(OpCodes.Ldstr, _ "This event handler was constructed at run time.") ilgen.Emit(OpCodes.Call, simpleShow) ilgen.Emit(OpCodes.Pop) ilgen.Emit(OpCodes.Ret)
呼叫 CreateDelegate 方法,以完成動態方法。 使用
add
存取子,以將委派新增至事件的叫用清單。Delegate^ dEmitted = handler->CreateDelegate(tDelegate); addHandler->Invoke(exFormAsObj, gcnew array<Object^> { dEmitted });
Delegate dEmitted = handler.CreateDelegate(tDelegate); addHandler.Invoke(exFormAsObj, new Object[] { dEmitted });
Dim dEmitted As [Delegate] = handler.CreateDelegate(tDelegate) miAddHandler.Invoke(exFormAsObj, New Object() {dEmitted})
測試事件。 下列程式碼會載入程式碼範例中所定義的表單。 按一下表單,會叫用預先定義的事件處理常式和發出的事件處理常式。
Application::Run((Form^) exFormAsObj);
Application.Run((Form) exFormAsObj);
Application.Run(CType(exFormAsObj, Form))
範例
下列程式碼範例示範如何使用反映以將現有方法連結至事件,同時示範如何使用 DynamicMethod 類別以在執行階段發出方法,並將它連結至事件。
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Windows::Forms;
public ref class ExampleForm : public Form
{
public:
ExampleForm() : Form()
{
this->Text = "Click me";
}
};
public ref class Example
{
public:
static void Main()
{
Example^ ex = gcnew Example();
ex->HookUpDelegate();
}
private:
void HookUpDelegate()
{
// Load an assembly, for example using the Assembly.Load
// method. In this case, the executing assembly is loaded, to
// keep the demonstration simple.
//
Assembly^ assem = Example::typeid->Assembly;
// Get the type that is to be loaded, and create an instance
// of it. Activator::CreateInstance has other overloads, if
// the type lacks a default constructor. The new instance
// is stored as type Object, to maintain the fiction that
// nothing is known about the assembly. (Note that you can
// get the types in an assembly without knowing their names
// in advance.)
//
Type^ tExForm = assem->GetType("ExampleForm");
Object^ exFormAsObj = Activator::CreateInstance(tExForm);
// Get an EventInfo representing the Click event, and get the
// type of delegate that handles the event.
//
EventInfo^ evClick = tExForm->GetEvent("Click");
Type^ tDelegate = evClick->EventHandlerType;
// If you already have a method with the correct signature,
// you can simply get a MethodInfo for it.
//
MethodInfo^ miHandler =
Type::GetType("Example")->GetMethod("LuckyHandler",
BindingFlags::NonPublic | BindingFlags::Instance);
// Create an instance of the delegate. Using the overloads
// of CreateDelegate that take MethodInfo is recommended.
//
Delegate^ d = Delegate::CreateDelegate(tDelegate, this, miHandler);
// Get the "add" accessor of the event and invoke it late-
// bound, passing in the delegate instance. This is equivalent
// to using the += operator in C#, or AddHandler in Visual
// Basic. The instance on which the "add" accessor is invoked
// is the form; the arguments must be passed as an array.
//
MethodInfo^ addHandler = evClick->GetAddMethod();
array<Object^>^ addHandlerArgs = { d };
addHandler->Invoke(exFormAsObj, addHandlerArgs);
// Event handler methods can also be generated at run time,
// using lightweight dynamic methods and Reflection.Emit.
// To construct an event handler, you need the return type
// and parameter types of the delegate. These can be obtained
// by examining the delegate's Invoke method.
//
// It is not necessary to name dynamic methods, so the empty
// string can be used. The last argument associates the
// dynamic method with the current type, giving the delegate
// access to all the public and private members of Example,
// as if it were an instance method.
//
Type^ returnType = GetDelegateReturnType(tDelegate);
if (returnType != void::typeid)
throw gcnew ApplicationException("Delegate has a return type.");
DynamicMethod^ handler =
gcnew DynamicMethod("",
nullptr,
GetDelegateParameterTypes(tDelegate),
Example::typeid);
// Generate a method body. This method loads a string, calls
// the Show method overload that takes a string, pops the
// return value off the stack (because the handler has no
// return type), and returns.
//
ILGenerator^ ilgen = handler->GetILGenerator();
array<Type^>^ showParameters = { String::typeid };
MethodInfo^ simpleShow =
MessageBox::typeid->GetMethod("Show", showParameters);
ilgen->Emit(OpCodes::Ldstr,
"This event handler was constructed at run time.");
ilgen->Emit(OpCodes::Call, simpleShow);
ilgen->Emit(OpCodes::Pop);
ilgen->Emit(OpCodes::Ret);
// Complete the dynamic method by calling its CreateDelegate
// method. Use the "add" accessor to add the delegate to
// the invocation list for the event.
//
Delegate^ dEmitted = handler->CreateDelegate(tDelegate);
addHandler->Invoke(exFormAsObj, gcnew array<Object^> { dEmitted });
// Show the form. Clicking on the form causes the two
// delegates to be invoked.
//
Application::Run((Form^) exFormAsObj);
}
void LuckyHandler(Object^ sender, EventArgs^ e)
{
MessageBox::Show("This event handler just happened to be lying around.");
}
array<Type^>^ GetDelegateParameterTypes(Type^ d)
{
if (d->BaseType != MulticastDelegate::typeid)
throw gcnew ApplicationException("Not a delegate.");
MethodInfo^ invoke = d->GetMethod("Invoke");
if (invoke == nullptr)
throw gcnew ApplicationException("Not a delegate.");
array<ParameterInfo^>^ parameters = invoke->GetParameters();
array<Type^>^ typeParameters = gcnew array<Type^>(parameters->Length);
for (int i = 0; i < parameters->Length; i++)
{
typeParameters[i] = parameters[i]->ParameterType;
}
return typeParameters;
}
Type^ GetDelegateReturnType(Type^ d)
{
if (d->BaseType != MulticastDelegate::typeid)
throw gcnew ApplicationException("Not a delegate.");
MethodInfo^ invoke = d->GetMethod("Invoke");
if (invoke == nullptr)
throw gcnew ApplicationException("Not a delegate.");
return invoke->ReturnType;
}
};
int main()
{
Example::Main();
}
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Windows.Forms;
class ExampleForm : Form
{
public ExampleForm() : base()
{
this.Text = "Click me";
}
}
class Example
{
public static void Main()
{
Example ex = new Example();
ex.HookUpDelegate();
}
private void HookUpDelegate()
{
// Load an assembly, for example using the Assembly.Load
// method. In this case, the executing assembly is loaded, to
// keep the demonstration simple.
//
Assembly assem = typeof(Example).Assembly;
// Get the type that is to be loaded, and create an instance
// of it. Activator.CreateInstance has other overloads, if
// the type lacks a default constructor. The new instance
// is stored as type Object, to maintain the fiction that
// nothing is known about the assembly. (Note that you can
// get the types in an assembly without knowing their names
// in advance.)
//
Type tExForm = assem.GetType("ExampleForm");
Object exFormAsObj = Activator.CreateInstance(tExForm);
// Get an EventInfo representing the Click event, and get the
// type of delegate that handles the event.
//
EventInfo evClick = tExForm.GetEvent("Click");
Type tDelegate = evClick.EventHandlerType;
// If you already have a method with the correct signature,
// you can simply get a MethodInfo for it.
//
MethodInfo miHandler =
typeof(Example).GetMethod("LuckyHandler",
BindingFlags.NonPublic | BindingFlags.Instance);
// Create an instance of the delegate. Using the overloads
// of CreateDelegate that take MethodInfo is recommended.
//
Delegate d = Delegate.CreateDelegate(tDelegate, this, miHandler);
// Get the "add" accessor of the event and invoke it late-
// bound, passing in the delegate instance. This is equivalent
// to using the += operator in C#, or AddHandler in Visual
// Basic. The instance on which the "add" accessor is invoked
// is the form; the arguments must be passed as an array.
//
MethodInfo addHandler = evClick.GetAddMethod();
Object[] addHandlerArgs = { d };
addHandler.Invoke(exFormAsObj, addHandlerArgs);
// Event handler methods can also be generated at run time,
// using lightweight dynamic methods and Reflection.Emit.
// To construct an event handler, you need the return type
// and parameter types of the delegate. These can be obtained
// by examining the delegate's Invoke method.
//
// It is not necessary to name dynamic methods, so the empty
// string can be used. The last argument associates the
// dynamic method with the current type, giving the delegate
// access to all the public and private members of Example,
// as if it were an instance method.
//
Type returnType = GetDelegateReturnType(tDelegate);
if (returnType != typeof(void))
throw new ArgumentException("Delegate has a return type.", nameof(d));
DynamicMethod handler =
new DynamicMethod("",
null,
GetDelegateParameterTypes(tDelegate),
typeof(Example));
// Generate a method body. This method loads a string, calls
// the Show method overload that takes a string, pops the
// return value off the stack (because the handler has no
// return type), and returns.
//
ILGenerator ilgen = handler.GetILGenerator();
Type[] showParameters = { typeof(String) };
MethodInfo simpleShow =
typeof(MessageBox).GetMethod("Show", showParameters);
ilgen.Emit(OpCodes.Ldstr,
"This event handler was constructed at run time.");
ilgen.Emit(OpCodes.Call, simpleShow);
ilgen.Emit(OpCodes.Pop);
ilgen.Emit(OpCodes.Ret);
// Complete the dynamic method by calling its CreateDelegate
// method. Use the "add" accessor to add the delegate to
// the invocation list for the event.
//
Delegate dEmitted = handler.CreateDelegate(tDelegate);
addHandler.Invoke(exFormAsObj, new Object[] { dEmitted });
// Show the form. Clicking on the form causes the two
// delegates to be invoked.
//
Application.Run((Form) exFormAsObj);
}
private void LuckyHandler(Object sender, EventArgs e)
{
MessageBox.Show("This event handler just happened to be lying around.");
}
private Type[] GetDelegateParameterTypes(Type d)
{
if (d.BaseType != typeof(MulticastDelegate))
throw new ArgumentException("Not a delegate.", nameof(d));
MethodInfo invoke = d.GetMethod("Invoke");
if (invoke == null)
throw new ArgumentException("Not a delegate.", nameof(d));
ParameterInfo[] parameters = invoke.GetParameters();
Type[] typeParameters = new Type[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
typeParameters[i] = parameters[i].ParameterType;
}
return typeParameters;
}
private Type GetDelegateReturnType(Type d)
{
if (d.BaseType != typeof(MulticastDelegate))
throw new ArgumentException("Not a delegate.", nameof(d));
MethodInfo invoke = d.GetMethod("Invoke");
if (invoke == null)
throw new ArgumentException("Not a delegate.", nameof(d));
return invoke.ReturnType;
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Windows.Forms
Class ExampleForm
Inherits Form
Public Sub New()
Me.Text = "Click me"
End Sub
End Class
Class Example
Public Shared Sub Main()
Dim ex As New Example()
ex.HookUpDelegate()
End Sub
Private Sub HookUpDelegate()
' Load an assembly, for example using the Assembly.Load
' method. In this case, the executing assembly is loaded, to
' keep the demonstration simple.
'
Dim assem As Assembly = GetType(Example).Assembly
' Get the type that is to be loaded, and create an instance
' of it. Activator.CreateInstance also has an overload that
' takes an array of types representing the types of the
' constructor parameters, if the type you are creating does
' not have a parameterless constructor. The new instance
' is stored as type Object, to maintain the fiction that
' nothing is known about the assembly. (Note that you can
' get the types in an assembly without knowing their names
' in advance.)
'
Dim tExForm As Type = assem.GetType("ExampleForm")
Dim exFormAsObj As Object = _
Activator.CreateInstance(tExForm)
' Get an EventInfo representing the Click event, and get the
' type of delegate that handles the event.
'
Dim evClick As EventInfo = tExForm.GetEvent("Click")
Dim tDelegate As Type = evClick.EventHandlerType
' If you already have a method with the correct signature,
' you can simply get a MethodInfo for it.
'
Dim miHandler As MethodInfo = _
GetType(Example).GetMethod("LuckyHandler", _
BindingFlags.NonPublic Or BindingFlags.Instance)
' Create an instance of the delegate. Using the overloads
' of CreateDelegate that take MethodInfo is recommended.
'
Dim d As [Delegate] = _
[Delegate].CreateDelegate(tDelegate, Me, miHandler)
' Get the "add" accessor of the event and invoke it late-
' bound, passing in the delegate instance. This is equivalent
' to using the += operator in C#, or AddHandler in Visual
' Basic. The instance on which the "add" accessor is invoked
' is the form; the arguments must be passed as an array.
'
Dim miAddHandler As MethodInfo = evClick.GetAddMethod()
Dim addHandlerArgs() As Object = {d}
miAddHandler.Invoke(exFormAsObj, addHandlerArgs)
' Event handler methods can also be generated at run time,
' using lightweight dynamic methods and Reflection.Emit.
' To construct an event handler, you need the return type
' and parameter types of the delegate. These can be obtained
' by examining the delegate's Invoke method.
'
' It is not necessary to name dynamic methods, so the empty
' string can be used. The last argument associates the
' dynamic method with the current type, giving the delegate
' access to all the public and private members of Example,
' as if it were an instance method.
'
Dim returnType As Type = GetDelegateReturnType(tDelegate)
If returnType IsNot GetType(Void) Then
Throw New ArgumentException("Delegate has a return type.", NameOf(d))
End If
Dim handler As New DynamicMethod( _
"", _
Nothing, _
GetDelegateParameterTypes(tDelegate), _
GetType(Example) _
)
' Generate a method body. This method loads a string, calls
' the Show method overload that takes a string, pops the
' return value off the stack (because the handler has no
' return type), and returns.
'
Dim ilgen As ILGenerator = handler.GetILGenerator()
Dim showParameters As Type() = {GetType(String)}
Dim simpleShow As MethodInfo = _
GetType(MessageBox).GetMethod("Show", showParameters)
ilgen.Emit(OpCodes.Ldstr, _
"This event handler was constructed at run time.")
ilgen.Emit(OpCodes.Call, simpleShow)
ilgen.Emit(OpCodes.Pop)
ilgen.Emit(OpCodes.Ret)
' Complete the dynamic method by calling its CreateDelegate
' method. Use the "add" accessor to add the delegate to
' the invocation list for the event.
'
Dim dEmitted As [Delegate] = handler.CreateDelegate(tDelegate)
miAddHandler.Invoke(exFormAsObj, New Object() {dEmitted})
' Show the form. Clicking on the form causes the two
' delegates to be invoked.
'
Application.Run(CType(exFormAsObj, Form))
End Sub
Private Sub LuckyHandler(ByVal sender As [Object], _
ByVal e As EventArgs)
MessageBox.Show("This event handler just happened to be lying around.")
End Sub
Private Function GetDelegateParameterTypes(ByVal d As Type) _
As Type()
If d.BaseType IsNot GetType(MulticastDelegate) Then
Throw New ArgumentException("Not a delegate.", NameOf(d))
End If
Dim invoke As MethodInfo = d.GetMethod("Invoke")
If invoke Is Nothing Then
Throw New ArgumentException("Not a delegate.", NameOf(d))
End If
Dim parameters As ParameterInfo() = invoke.GetParameters()
' Dimension this array Length - 1, because VB adds an extra
' element to zero-based arrays.
Dim typeParameters(parameters.Length - 1) As Type
For i As Integer = 0 To parameters.Length - 1
typeParameters(i) = parameters(i).ParameterType
Next i
Return typeParameters
End Function
Private Function GetDelegateReturnType(ByVal d As Type) As Type
If d.BaseType IsNot GetType(MulticastDelegate) Then
Throw New ArgumentException("Not a delegate.", NameOf(d))
End If
Dim invoke As MethodInfo = d.GetMethod("Invoke")
If invoke Is Nothing Then
Throw New ArgumentException("Not a delegate.", NameOf(d))
End If
Return invoke.ReturnType
End Function
End Class