为什么无法使用新的PingReply?

时间:2019-07-07 15:15:32

标签: c# .net

在使用System.Net.NetworkInformation.Ping对象的SendPingAsync方法时,尝试对无法解析的主机名(即localhostnowhere)执行ping操作时,它将抛出PingException异常。

但是当捕获异常时,不可能重新创建PingReply吗?

  PingReply pingReply = null;
  try
  {
    pingReply = await pingSender.SendPingAsync(host, timeout, buffer, options);
  }
  catch (PingException ex)
  {
    pingReply = new PingReply();
    //other pingReply properties set here, like status <> success, etc
  }
  catch (ArgumentNullException) { }
  catch (Exception) { throw; }

在查看PingReply的定义时,它没有默认的构造函数。它没有任何构造函数。

#region Assembly System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.dll
#endregion

namespace System.Net.NetworkInformation
{
  //
  // Summary:
  //     Provides information about the status and data resulting from a Overload:System.Net.NetworkInformation.Ping.Send
  //     or Overload:System.Net.NetworkInformation.Ping.SendAsync operation.
  public class PingReply
  {
    //
    // Summary:
    //     Gets the status of an attempt to send an Internet Control Message Protocol (ICMP)
    //     echo request and receive the corresponding ICMP echo reply message.
    //
    // Returns:
    //     An System.Net.NetworkInformation.IPStatus value indicating the result of the
    //     request.
    public IPStatus Status { get; }
    //
    // Summary:
    //     Gets the address of the host that sends the Internet Control Message Protocol
    //     (ICMP) echo reply.
    //
    // Returns:
    //     An System.Net.IPAddress containing the destination for the ICMP echo message.
    public IPAddress Address { get; }
    //
    // Summary:
    //     Gets the number of milliseconds taken to send an Internet Control Message Protocol
    //     (ICMP) echo request and receive the corresponding ICMP echo reply message.
    //
    // Returns:
    //     An System.Int64 that specifies the round trip time, in milliseconds.
    public long RoundtripTime { get; }
    //
    // Summary:
    //     Gets the options used to transmit the reply to an Internet Control Message Protocol
    //     (ICMP) echo request.
    //
    // Returns:
    //     A System.Net.NetworkInformation.PingOptions object that contains the Time to
    //     Live (TTL) and the fragmentation directive used for transmitting the reply if
    //     System.Net.NetworkInformation.PingReply.Status is System.Net.NetworkInformation.IPStatus.Success;
    //     otherwise, null.
    public PingOptions Options { get; }
    //
    // Summary:
    //     Gets the buffer of data received in an Internet Control Message Protocol (ICMP)
    //     echo reply message.
    //
    // Returns:
    //     A System.Byte array containing the data received in an ICMP echo reply message,
    //     or an empty array, if no reply was received.
    public byte[] Buffer { get; }
  }
}

在ping不成功的情况下,如何返回PingReply? Ping如何返回新的PingReply?

1 个答案:

答案 0 :(得分:2)

PingReply构造函数定义为internal和

  

内部类型或成员只能在同一程序集https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal

中的文件中访问

那样一来,由于您的代码不在同一程序集中,而Ping在同一程序集中,因此您无法调用它们。
如果根本没有执行SendPingAsync,您将获得一个异常。如果已执行,则可以检查pingReply.Status。