任何避免在这种情况下明确声明MyMethodDelegate
的方法?C# - 方法的自动委托类型
bool MyMethod(string x)
{
//...
}
BeginInvoke(new MyMethodDelegate(MyMethod), x);
我知道lambda表达式一拉()=>MyMethod(x)
,但是我想,以避免它们有时他们打破编辑和继续。
编辑:只是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)
打我吧。尽管按照OP,'int'应该是一个'string'。 –