2014-05-14 48 views
0

我想在VB.NET中編寫C#中的Task.Factory.FromAsync實現。VB.NET中的Task.Factory.FromAsync語法

這是C#語法。

public static Task<int> SendAsync(this Socket socket, byte[] buffer, int offset, int count) 
{ 
     return Task.Factory.FromAsync<int>(
         socket.BeginSend(buffer, offset, count, SocketFlags.None, null, socket), 
       socket.EndSend); 
} 

我把它轉換成在VB.NET

以下
Public Shared Function SendAsync(socket As Socket, buffer As Byte(), offset As Integer, count As Integer) As Task(Of Integer) 
    Return Task.Factory.FromAsync(Of Integer)(socket.BeginSend(buffer, offset, count, SocketFlags.None, Nothing, socket), socket.EndSend) 
End Function 

然而,在VB.NET語法,我碰到下面的錯誤。此代碼在C#中完美工作。

Overload resolution failed because no accessible 'EndSend' accepts this number of arguments. 

我錯過了什麼嗎?

回答

2

您必須使用AddressOf operator

Public Shared Function SendAsync(socket As Socket, buffer As Byte(), offset As Integer, count As Integer) As Task(Of Integer) 
    Return Task.Factory.FromAsync(...(...), AddressOf socket.EndSend) 
End Function 

由於可以省略()調用VB.Net的方法時,編譯器不知道要使用socket.EndSend作爲一個代表,而不是調用的它因此期望它返回一個Func(Of IAsyncResult, Integer),它不會這樣做。因此,重載分辨率錯誤。

+0

工作正常!謝謝 :) – Dhiru