这可能是一个愚蠢的问题,但是,我有一个方法可以使页面的语法更易于阅读
public void Do(Delegate method, DispatcherPriority priority = DispatcherPriority.Normal)
{
this.Window.Dispatcher.BeginInvoke(method, DispatcherPriority.Background);
}然后我就可以写了
Do(new Action(() =>
{
//DoStuff()
}));但是,我想将Action上移到Do方法中,这样我就可以编写更简单的代码:
Do(() =>
{
//DoStuff()
}));但我有点确定如何编写逆变量参数来执行do方法?
发布于 2010-12-20 20:05:19
Lambda是无类型的,所以这是不可能的。
如果您不关心方法参数,为什么不将方法签名更改为:
public void Do(Action method,
DispatcherPriority priority = DispatcherPriority.Normal)然后,第二个示例将工作得很好,因为编译器将能够隐式地将lambda转换为Action。
如果你真的想接受一个代表不带参数的方法的委托类型的实例,你必须坚持你当前拥有的东西。
https://stackoverflow.com/questions/4489414
复制相似问题