如何在订阅事件时传递值并在事件触发时获取(DynamicMethod使用问题)
任务是在运行时创建事件处理程序。我需要针对不同的事件使用不同的参数值调用一种方法。事件及其数量仅在运行时已知。因此,我尝试生成动态方法,每个方法都将分配给某个事件,但一般来说,它们都只是将一些值传递给实例方法并调用它。
如果类似的事情能够以更简单的方式完成,那就太好了。我的意思是在订阅阶段传递一些值,然后在事件触发时获取它。
这就是我现在正在尝试做的:
public class EventSource
{
public event EventHandler eventOne;
public event EventHandler eventTwO;
public event EventHandler eventThree;
}
public class EventListener
{
SubscribeForEvents()
{
BindingFlags flags =
BindingFlags.IgnoreCase |
BindingFlags.Public |
BindingFlags.Instance;
// Suppose we've already got EventInfo
// and target source somewhere
// so we can do eventInfo.AddEventHandler(target, delegate)
// Now we need a delegate.
int value = 42;
Type tDelegate = eventInfo.EventHandlerType;
// http://msdn.microsoft.com/en-us/library/ms228976(VS.95).aspx
Type returnType = GetDelegateReturnType(tDelegate);
DynamicMethod listener = new DynamicMethod("", null,
GetDelegateParameterTypes(tDelegate), this.GetType());
/////////
Type[] callParameters = { typeof(int) };
MethodInfo method = this.GetType().GetMethod("ToCallFromDelegate", flags);
ILGenerator generator = listener.GetILGenerator();
// No success in this mess. What's wrong?
generator.Emit(OpCodes.Ldc_I4, value);
generator.Emit(OpCodes.Call, method);
generator.Emit(OpCodes.Pop);
generator.Emit(OpCodes.Ret);
/////////////
Delegate delegate = listener.CreateDelegate(tDelegate);
eventInfo.AddEventHandler(target, delegate);
// When triggered, there is InvalidProgramException
}
void ToCallFromDelegate(int value)
{
doSomething();
}
}
The task is to create event handlers in runtime. I need the one method to be called with different parameter value for different events. The events and their number are only known in runtime. So I'm trying to generate dynamic methods, each of which will be assigned to some event, but in general they all just pass some value to an instance method and call it.
It would be great if something similar could be done the easier way. I mean passing some value at subscribing stage and then obtaining it when the event is triggered.
This is what I'm trying to do now:
public class EventSource
{
public event EventHandler eventOne;
public event EventHandler eventTwO;
public event EventHandler eventThree;
}
public class EventListener
{
SubscribeForEvents()
{
BindingFlags flags =
BindingFlags.IgnoreCase |
BindingFlags.Public |
BindingFlags.Instance;
// Suppose we've already got EventInfo
// and target source somewhere
// so we can do eventInfo.AddEventHandler(target, delegate)
// Now we need a delegate.
int value = 42;
Type tDelegate = eventInfo.EventHandlerType;
// http://msdn.microsoft.com/en-us/library/ms228976(VS.95).aspx
Type returnType = GetDelegateReturnType(tDelegate);
DynamicMethod listener = new DynamicMethod("", null,
GetDelegateParameterTypes(tDelegate), this.GetType());
/////////
Type[] callParameters = { typeof(int) };
MethodInfo method = this.GetType().GetMethod("ToCallFromDelegate", flags);
ILGenerator generator = listener.GetILGenerator();
// No success in this mess. What's wrong?
generator.Emit(OpCodes.Ldc_I4, value);
generator.Emit(OpCodes.Call, method);
generator.Emit(OpCodes.Pop);
generator.Emit(OpCodes.Ret);
/////////////
Delegate delegate = listener.CreateDelegate(tDelegate);
eventInfo.AddEventHandler(target, delegate);
// When triggered, there is InvalidProgramException
}
void ToCallFromDelegate(int value)
{
doSomething();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Lambda 作为事件处理程序;)
Lambdas as event handlers ;)