Take the method System.Windows.Forms.Control.Invoke(Delegate method)
Why does this give a compile time error:
string str = "woop";
Invoke(() => this.Text = str);
// Error: Cannot convert lambda expression to type 'System.Delegate'
// because it is not a delegate type
Yet this works fine:
string str = "woop";
Invoke((Action)(() => this.Text = str));
When the method expects a plain Delegate?
Best Solution
A lambda expression can either be converted to a delegate type or an expression tree - but it has to know which delegate type. Just knowing the signature isn't enough. For instance, suppose I have:
What would you expect the concrete type of the object referred to by
x
to be? Yes, the compiler could generate a new delegate type with an appropriate signature, but that's rarely useful and you end up with less opportunity for error checking.If you want to make it easy to call
Control.Invoke
with anAction
the easiest thing to do is add an extension method to Control: