2014-09-13 66 views
0

我試圖使用Stephen Toub的文章中找到的SocketAwaitable類; http://blogs.msdn.com/b/pfxteam/archive/2011/12/15/10248293.aspx如何將此SocketAwaitable類從C#轉換爲VB?

我已經使用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?)

任何人都可以幫助我進行此轉換嗎?

+1

行動='Sub',函數功能='Function'。嘗試'新的行動(小())' – 2014-09-13 18:21:46

回答

2

嘗試使用以下 - 刪除的RaiseEvent和空拉姆達應該是一個 '子' 拉姆達:

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