我在C#中有一个Task.Factory.FromAsync的实现,我想在VB.NET中编写。
这是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.
我错过了什么吗?
答案 0 :(得分: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)
,它没有做到。因此,重载分辨率错误。