5
在MSDN上提到GetHostByName
已過時。替代品是GetHostEntry
。他們有什麼不同?GetHostEntry和GetHostByName的區別?
在MSDN上提到GetHostByName
已過時。替代品是GetHostEntry
。他們有什麼不同?GetHostEntry和GetHostByName的區別?
它看起來像GetHostEntry做多一點錯誤檢查,同時還支持Network Tracing
的gethostbyname反編譯:
public static IPHostEntry GetHostByName(string hostName)
{
if (hostName == null)
throw new ArgumentNullException("hostName");
Dns.s_DnsPermission.Demand();
IPAddress address;
if (IPAddress.TryParse(hostName, out address))
return Dns.GetUnresolveAnswer(address);
else
return Dns.InternalGetHostByName(hostName, false);
}
GetHostEntry反編譯:
public static IPHostEntry GetHostEntry(string hostNameOrAddress)
{
if (Logging.On)
Logging.Enter(Logging.Sockets, "DNS", "GetHostEntry", hostNameOrAddress);
Dns.s_DnsPermission.Demand();
if (hostNameOrAddress == null)
throw new ArgumentNullException("hostNameOrAddress");
IPAddress address;
IPHostEntry ipHostEntry;
if (IPAddress.TryParse(hostNameOrAddress, out address))
{
if (((object) address).Equals((object) IPAddress.Any) || ((object) address).Equals((object) IPAddress.IPv6Any))
throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "hostNameOrAddress");
ipHostEntry = Dns.InternalGetHostByAddress(address, true);
}
else
ipHostEntry = Dns.InternalGetHostByName(hostNameOrAddress, true);
if (Logging.On)
Logging.Exit(Logging.Sockets, "DNS", "GetHostEntry", (object) ipHostEntry);
return ipHostEntry;
}
如果你發現自己與那些過時的誤差只是和我一樣,只需在代碼中用'GetHostEntry()'方便地更改'GetHostByName()'。 – Aryo 2013-09-01 14:28:40