我需要在运行时基于 EventInfo 对象生成一个事件处理程序,并在该事件处理程序中调用一个方法。类似于以下内容:
public void RegisterAction(ActionData actionData, EventInfo eventInfo,
Control control)
{
MethodInfo methodInfo = eventInfo.EventHandlerType.GetMethod("Invoke");
List<Type> ps = new List<Type>();
foreach (ParameterInfo info in methodInfo.GetParameters())
{
ps.Add(info.ParameterType);
}
DynamicMethod method = new DynamicMethod("Adapter",
typeof (void),
ps.ToArray(),
GetType(),
true);
ILGenerator generator = method.GetILGenerator();
// Here I need to generate a method to do the following:
// ExecuteAction(actionData);
// Then I can use this runtime method as an event handler and
// bind it to the control
Delegate proxy = method.CreateDelegate(eventInfo.EventHandlerType, this);
eventInfo.AddEventHandler(control, proxy);
}
我需要帮助来生成注释部分的 IL 代码。