如何在订阅事件时传递值并在事件触发时获取(DynamicMethod使用问题)

发布于 2024-08-31 13:22:41 字数 1839 浏览 12 评论 0原文

任务是在运行时创建事件处理程序。我需要针对不同的事件使用不同的参数值调用一种方法。事件及其数量仅在运行时已知。因此,我尝试生成动态方法,每个方法都将分配给某个事件,但一般来说,它们都只是将一些值传递给实例方法并调用它。

如果类似的事情能够以更简单的方式完成,那就太好了。我的意思是在订阅阶段传递一些值,然后在事件触发时获取它。

这就是我现在正在尝试做的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2024-09-07 13:22:41

Lambda 作为事件处理程序;)

Lambdas as event handlers ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文