IL Emit for invoking a delegate instance?

前端 未结 2 403
花落未央
花落未央 2021-01-12 15:47

Basically, I\'m accepting an event name as a string, to get the EventInfo. Then, I\'m discovering the event handler type and event argument type using reflectio

2条回答
  •  佛祖请我去吃肉
    2021-01-12 16:23

    It turns out I was vastly over-complicating things! Barry Kelly had the right idea:

    static T CastDelegate(Delegate src)
        where T : class
    {
        return (T)(object)Delegate.CreateDelegate(
            typeof(T),
            src.Target,
            src.Method,
            true); // throw on fail
    }
    

    That works for my test cases.

提交回复
热议问题