One of the annoying things about delegates in .NET is that delegates with exactly the same parameters and return type are not compatible. Specifically, you cannot cast a delegate to a delegate of another type even if they have the same parameters and return type.
This problem is mitigated somewhat in C# 3.5, which defines generic delegates that take arbitrary parameters and return types and encourages their use: Action, Action<T>, Action<T1, T2>, …, Func<TR>, Func<T, TR>, Func<T1, T2, TR>, …
The biggest source of delegate types is event handlers. There’s plain old EventHandler and the newer EventHandler<T>, but there are still lots of non-generic event handlers like CancelEventHandler. Neither WPF nor Windows Forms use EventHandler<T>, so they are chock full of unique delegate types that take an object and some EventArgs-derived class.
Usually this doesn’t present a problem, but occasionally you’d like to convert between compatible delegates. If both types are known at compile-time, you can just use a lambda:
But sometimes, the types aren’t known at compile-time. We primarily find this to be the case when trying to write generic utility code that can work with arbitrary event handlers. Fortunately, it is possible to cast between arbitrary delegate types, though it isn’t as efficient as you might like - DelegateUtility.Cast:
There is a generic version and a non-generic version. Note that the null case is handled first, followed by the single-invocation case, followed by the rare multiple-invocation case.
It is quite straightforward to use. (We’d have made it an extension method, but converting delegates isn’t really a common enough need to justify it.)
The types used by the two delegate types must be exactly the same for DelegateUtility.Cast to work. Supporting compatible types is left as an exercise for the reader; we certainly haven’t needed it.
Posted by Ed Ball on July 28, 2008