I would like to know if there is some way to cast method group, anonymous method or lambda expression to System.Delegate without knowing exact delegate type, like
var d = ()=>{};
dynamic d = ()=>{};
Delegate d = ()=>{};
I need a way that would work for any method with any parameters and return type and not using reflection.
Thanks for your answers.
No it is not possible, because a delegate must have a single defined type, but multiple delegates can have the same signature. This makes the delegate type ambiguous – the compiler would have no way of robustly selecting an appropriate type in such a statement. Note that delegate instances of matching-signatures but different types are not interchangeable. For example, is:
A
Func<int,bool>, or aPredicate<int>? Or something else? Note that the number of generic arts is irrelevant; anActionandThreadStartandMethodInvokerare all just void non-generic parameterless delegates.Additionally, there’s an added complication / ambiguity because lambdas can also be compiled to expression trees, not just delegates:
Expression trees are radically different to delegates.