How to convert delegate to identical delegate?

后端 未结 5 2036
你的背包
你的背包 2021-02-18 19:04

There are two descriptions of the delegate: first, in a third-party assembly:

public delegate void ClickMenuItem (object sender, EventArgs e)

s

5条回答
  •  执念已碎
    2021-02-18 19:08

    In addition to other answers, if you want to do convert between compatible delegate types without knowing the type at compile time, you can do something like that:

    static Delegate ConvertDelegate(Delegate sourceDelegate, Type targetType)
    {
        return Delegate.CreateDelegate(
                targetType,
                sourceDelegate.Target,
                sourceDelegate.Method);
    }
    

    It can be useful if you need to subscribe to an event dynamically.

提交回复
热议问题