任何避免在這種情況下明確聲明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'。 –