[英]How can I convert this SocketAwaitable class from C# to VB?
我正在嘗試使用Stephen Toub的文章中的SocketAwaitable類。 http://blogs.msdn.com/b/pfxteam/archive/2011/12/15/10248293.aspx
我已經使用此處找到的DeveloperFusions工具將其轉換為VB.NET。 http://www.developerfusion.com/tools/convert/csharp-to-vb/
它給我的代碼是;
Public NotInheritable Class SocketAwaitable
Implements INotifyCompletion
Private Shared ReadOnly SENTINEL As Action = Function()
End Function
Friend m_wasCompleted As Boolean
Friend m_continuation As Action
Friend m_eventArgs As SocketAsyncEventArgs
Public Sub New(eventArgs As SocketAsyncEventArgs)
If eventArgs Is Nothing Then
Throw New ArgumentNullException("eventArgs")
End If
m_eventArgs = eventArgs
AddHandler eventArgs.Completed, Sub()
Dim prev As action = If(m_continuation, Interlocked.CompareExchange(m_continuation, SENTINEL, Nothing))
RaiseEvent prev()
End Sub
End Sub
Friend Sub Reset()
m_wasCompleted = False
m_continuation = Nothing
End Sub
Public Function GetAwaiter() As SocketAwaitable
Return Me
End Function
Public ReadOnly Property IsCompleted() As Boolean
Get
Return m_wasCompleted
End Get
End Property
Public Sub OnCompleted(continuation As Action) Implements INotifyCompletion.OnCompleted
If m_continuation = SENTINEL OrElse Interlocked.CompareExchange(m_continuation, continuation, Nothing) = SENTINEL Then
Tasks.Task.Run(continuation)
End If
End Sub
Public Sub GetResult()
If m_eventArgs.SocketError <> SocketError.Success Then
Throw New SocketException(CInt(m_eventArgs.SocketError))
End If
End Sub
End Class
但是,轉換后有一些錯誤,我不確定該如何解決。 特別...
Private Shared ReadOnly SENTINEL As Action = Function()
End Function
...給我一個“無法推斷類型。考慮添加'As'子句以指定返回類型”。 也...
AddHandler eventArgs.Completed, Sub()
Dim prev As action = If(m_continuation, Interlocked.CompareExchange(m_continuation, SENTINEL, Nothing))
RaiseEvent prev()
End Sub
...“ prev”錯誤不是SocketAwaitable事件。 (也許我可以刪除RaiseEvent嗎?)
有人可以幫我進行這種轉換嗎?
請嘗試以下操作-刪除RaiseEvent,並且空的lambda應該是“ Sub” lambda:
Public NotInheritable Class SocketAwaitable
Implements INotifyCompletion
Private ReadOnly Shared SENTINEL As Action = Sub()
End Sub
Friend m_wasCompleted As Boolean
Friend m_continuation As Action
Friend m_eventArgs As SocketAsyncEventArgs
Public Sub New(ByVal eventArgs As SocketAsyncEventArgs)
If eventArgs Is Nothing Then
Throw New ArgumentNullException("eventArgs")
End If
m_eventArgs = eventArgs
AddHandler eventArgs.Completed, Sub()
Dim prev = If(m_continuation, Interlocked.CompareExchange(m_continuation, SENTINEL, Nothing))
If prev IsNot Nothing Then
prev()
End If
End Sub
End Sub
Friend Sub Reset()
m_wasCompleted = False
m_continuation = Nothing
End Sub
Public Function GetAwaiter() As SocketAwaitable
Return Me
End Function
Public ReadOnly Property IsCompleted() As Boolean
Get
Return m_wasCompleted
End Get
End Property
Public Sub OnCompleted(ByVal continuation As Action)
If m_continuation Is SENTINEL OrElse Interlocked.CompareExchange(m_continuation, continuation, Nothing) Is SENTINEL Then
Task.Run(continuation)
End If
End Sub
Public Sub GetResult()
If m_eventArgs.SocketError <> SocketError.Success Then
Throw New SocketException(CInt(Math.Truncate(m_eventArgs.SocketError)))
End If
End Sub
End Class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.