如何将这个SocketAwaable类从C#转换为VB

本文关键字:转换 VB 类从 SocketAwaable | 更新日期: 2023-09-27 18:21:42

我正在尝试使用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

带有"'rev'不是SocketAwaitable的事件"的错误。(也许我可以删除RaiseEvent?)

有人能帮我转换一下吗?

如何将这个SocketAwaable类从C#转换为VB

尝试以下操作-删除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