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:
In the meanwhile, casting did the trick.
Before:
After:
“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)));