任何避免显式声明的方法MyMethodDelegate
在这样的场景下?
bool MyMethod(string x)
{
//...
}
BeginInvoke(new MyMethodDelegate(MyMethod), x);
我了解 lambdas a-la()=>MyMethod(x)
,但是有时我想避免它们,因为它们会破坏编辑并继续。
Edit: just BeginInvoke(MyMethod, x)
不起作用:
The best overloaded method match for 'System.Windows.Forms.Control.BeginInvoke(System.Delegate, params object[])' has some invalid arguments
Argument 1: cannot convert from 'method group' to 'System.Delegate'
Argument 2: cannot convert from 'string' to 'object[]'
BeginInvoke 定义如下:
public IAsyncResult BeginInvoke(Delegate method, params object[] args);
它没有指定任何特定的委托类型,因此编译器无法检测要实例化的委托类型BeginInvoke(MyMethod. x)
对于框架 >= 3.5,您可以使用预定义的委托 Action 和 Func (根据您的情况)
BeginInvoke(new Func<int, bool>(MyMethod), x);
函数文档http://msdn.microsoft.com/ru-ru/library/bb549151.aspx http://msdn.microsoft.com/ru-ru/library/bb549151.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)