将c#代码转换为VB.NET的问题 - PingExtensions.cs示例

时间:2011-08-04 05:24:39

标签: c# vb.net

您好我已将此并行扩展c#代码示例转换为VB.NET

http://code.msdn.microsoft.com/Samples-for-Parallel-b4b76364/sourcecode?fileId=25353&pathId=215900242

使用Developerfusion工具here但是我遇到了多个错误,我无法通过有限的C#体验解决这些错误。

1)收到错误后我将System.Runtime.CompilerServices.Extension转换为Global.System.Runtime.CompilerServices.ExtensionAttribute,这是我能提出的最接近的错误,我在行上得到错误(26)

Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, tcs))

ping.SendAsync(address, timeout, tcs)没有产生值

2)196号线附近

handler = Function(sender, e) EAPCommon.HandleCompletion(tcs, e, Function() e.Reply, Function() ping.PingCompleted -= handler)

我在'ping.PingCompleted'上说错误

  

'公共事件PingCompleted(发件人为对象,e As   System.Net.NetworkInformation.PingCompletedEventArgs)'是一个事件,   并且不能直接调用。使用'RaiseEvent'语句来引发   一个事件。

任何建议将不胜感激。完整代码如下(注释已删除),原始来源

http://code.msdn.microsoft.com/Samples-for-Parallel-b4b76364/sourcecode?fileId=25353&pathId=215900242

using System.Threading.Tasks; 

namespace System.Net.NetworkInformation 
{ 
    /// <summary>Extension methods for working with Ping asynchronously.</summary> 
    public static class PingExtensions 
    { 

        public static Task<PingReply> SendTask(this Ping ping, IPAddress address, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, byte[] buffer, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, buffer, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, byte[] buffer, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, buffer, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, byte[] buffer, PingOptions options, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, buffer, options, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, buffer, options, tcs)); 
        } 


        private static Task<PingReply> SendTaskCore(Ping ping, object userToken, Action<TaskCompletionSource<PingReply>> sendAsync) 
        { 
            // Validate we're being used with a real smtpClient.  The rest of the arg validation 
            // will happen in the call to sendAsync. 
            if (ping == null) throw new ArgumentNullException("ping"); 

            // Create a TaskCompletionSource to represent the operation 
            var tcs = new TaskCompletionSource<PingReply>(userToken); 

            // Register a handler that will transfer completion results to the TCS Task 
            PingCompletedEventHandler handler = null; 
            handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Reply, () => ping.PingCompleted -= handler); 
            ping.PingCompleted += handler; 

            // Try to start the async operation.  If starting it fails (due to parameter validation) 
            // unregister the handler before allowing the exception to propagate. 
            try 
            { 
                sendAsync(tcs); 
            } 
            catch(Exception exc) 
            { 
                ping.PingCompleted -= handler; 
                tcs.TrySetException(exc); 
            } 

            // Return the task to represent the asynchronous operation 
            return tcs.Task; 
        } 
    } 
} 

编辑:这是转换后的VB代码:

Imports System.Threading.Tasks
Imports System.Runtime.CompilerServices
Imports System.Net.NetworkInformation
Imports System.Net
Imports System.ComponentModel



Namespace System.Net.NetworkInformation


    ''' <summary>Extension methods for working with Ping asynchronously.</summary> 
    Public Module PingExtensions
        Sub New()
        End Sub

        <Global.System.Runtime.CompilerServices.ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal address As IPAddress, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal hostNameOrAddress As String, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(hostNameOrAddress, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal address As IPAddress, ByVal timeout As Integer, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, timeout, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal hostNameOrAddress As String, ByVal timeout As Integer, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(hostNameOrAddress, timeout, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal address As IPAddress, ByVal timeout As Integer, ByVal buffer As Byte(), ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, timeout, buffer, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal hostNameOrAddress As String, ByVal timeout As Integer, ByVal buffer As Byte(), ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(hostNameOrAddress, timeout, buffer, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal address As IPAddress, ByVal timeout As Integer, ByVal buffer As Byte(), ByVal options As PingOptions, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, timeout, buffer, options, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal hostNameOrAddress As String, ByVal timeout As Integer, ByVal buffer As Byte(), ByVal options As PingOptions, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(hostNameOrAddress, timeout, buffer, options, tcs))
        End Function


        Private Function SendTaskCore(ByVal ping As Ping, ByVal userToken As Object, ByVal sendAsync As Action(Of TaskCompletionSource(Of PingReply))) As Task(Of PingReply)
            ' Validate we're being used with a real smtpClient.  The rest of the arg validation 
            ' will happen in the call to sendAsync. 
            If ping Is Nothing Then
                Throw New ArgumentNullException("ping")
            End If

            ' Create a TaskCompletionSource to represent the operation 
            Dim tcs = New TaskCompletionSource(Of PingReply)(userToken)

            ' Register a handler that will transfer completion results to the TCS Task 
            Dim handler As PingCompletedEventHandler = Nothing
            handler = Function(sender, e) EAPCommon.HandleCompletion(tcs, e, Function() e.Reply, Function() ping.PingCompleted -= handler)
            AddHandler ping.PingCompleted, handler

            ' Try to start the async operation.  If starting it fails (due to parameter validation) 
            ' unregister the handler before allowing the exception to propagate. 
            Try
                sendAsync(tcs)
            Catch exc As Exception
                RemoveHandler ping.PingCompleted, handler
                tcs.TrySetException(exc)
            End Try

            ' Return the task to represent the asynchronous operation 
            Return tcs.Task
        End Function

1 个答案:

答案 0 :(得分:1)

第一个问题 - 用Function(tcs)替换所有Sub(tcs)位 - 编译器是正确的,SendAsync不返回任何内容,无论如何,您正在尝试提供{ {1}},而不是Action


第二个问题 - 我们还没有Func的来源,但我认为最终的论点需要改为EAPCommon.HandleCompletion


Inline Subs只有introduced with Visual Basic 10(.NET 4/2010工具集),而你的转换器说它现在支持.NET 3.5 ,所以这可能就是为什么它做得这么糟糕的工作(尽管它产生的东西无论如何都不是有效的VB)