【问题标题】:Problem converting c# code to VB.NET - PingExtensions.cs sample将 c# 代码转换为 VB.NET 时出现问题 - PingExtensions.cs 示例
【发布时间】:2011-08-04 14:55:39
【问题描述】:

您好,我已将此并行扩展 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(sender As Object, 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

【问题讨论】:

  • 你能发布你的VB.NET代码吗?
  • 谢谢蒂姆,它现在在上面。

标签: c# vb.net


【解决方案1】:

第一个问题 - 将所有这些 Function(tcs) 位替换为 Sub(tcs) - 编译器是正确的,SendAsync 不返回任何内容,而且无论如何,您正在尝试提供 Action,而不是 @ 987654326@.


第二个问题 - 我们还没有 EAPCommon.HandleCompletion 的来源,但我认为最后一个参数需要更改为 Sub() RemoveHandler ping.PingCompleted,handler 之类的东西


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多