Using reflection to specify the type of a delegate (to attach to an event)?

后端 未结 2 1530
陌清茗
陌清茗 2021-01-20 10:40

What I effectively want to do is something like this (I realise this is not valid code):

// Attach the event.
try
{
    EventInfo e = mappings[name];
    (e.         


        
2条回答
  •  深忆病人
    2021-01-20 11:10

    You can use Delegate.CreateDelegate to accomplish your goal like this:

    public void RegisterHandler(string name)
    {
        EventInfo e = mappings[name];
        EventHandler handler = (s, raw) =>
            {
                func.Call(this, raw.GetParameters());
            };
        e.AddEventHandler(this, Delegate.CreateDelegate(e.EventHandlerType, null, handler.Method));
    }
    

提交回复
热议问题