【问题标题】:How can I convert this SocketAwaitable class from C# to VB?如何将此 SocketAwaitable 类从 C# 转换为 VB?
【发布时间】:2014-09-14 02:19:35
【问题描述】:

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

谁能帮我完成这个转换,好吗?

【问题讨论】:

  • 操作 = Sub,功能 = Function。试试New Action( Sub() )

标签: c# .net vb.net


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多