There are two descriptions of the delegate: first, in a third-party assembly:
public delegate void ClickMenuItem (object sender, EventArgs e)
s
Thomas Levesque's answer does not work well for some special cases. This is an improved version.
public static Delegate ConvertDelegate(this Delegate src, Type targetType, bool doTypeCheck)
{
//Is it null or of the same type as the target?
if (src == null || src.GetType() == targetType)
return src;
//Is it multiple cast?
return src.GetInvocationList().Count() == 1
? Delegate.CreateDelegate(targetType, src.Target, src.Method, doTypeCheck)
: src.GetInvocationList().Aggregate
(null, (current, d) => Delegate.Combine(current, ConvertDelegate(d, targetType, doTypeCheck)));
}
The benefit of the above code is it passes the following test
EventHandler e = (o,e)=>{}
var a = e.ConvertDelegate(typeof(Action
whilst
EventHandler e = (o,e)=>{}
var a = new Action
will fail.