2014-09-13 58 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