Thursday, June 7, 2012

Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type

Last week I tried to pass an anonymous method as a parameter to a method that excepted a delegate. As far as I know, an anonymous method is just an alternative syntax for a delegate. So I expected that it should work. However no luck, the compiler complained with the following error:
“Cannot convert anonymous method to type ‘System.Delegate’ because it is not a delegate type’.”
If anyone can explain me why this doesn’t work, you’re welcome.
In the meanwhile, casting did the trick.
Before:
 Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, () => this.Command.Execute(null)); //Does not compile

After:
 Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => this.Command.Execute(null)));

1 comment:

Kristof Mattei said...

http://msdn.microsoft.com/en-us/vstudio/jj131514.aspx
Could it be this?