This is probably a stupid question but, I have a method I use to make the syntax of a page a little more easy to read
public void Do(Delegate method, DispatcherPriority priority = DispatcherPriority.Normal)
{
this.Window.Dispatcher.BeginInvoke(method, DispatcherPriority.Background);
}
then I can write
Do(new Action(() =>
{
//DoStuff()
}));
However, I’d like to move the Action up into the Do Method so I can write more Simply:
Do(() =>
{
//DoStuff()
}));
But I’m a bit sure how to write the contravariant parameter to do the Do Method?
Lambdas are untyped, so this is not possible.
If you don’t care about method-arguments, which appears to be the case, why not change the method signature to :
Then, the second sample will work fine since the compiler will be able to implicitly convert the lambda to an
Action.If you really want to accept an instance of any delegate-type that represents a method that takes no arguments, you’ll have to stick to something like what you currently have.