将 IP 地址的八进制表示转换为无符号整数(包含在 long 中)。

public static long CastIp(string ip)
{
    return (long)(uint)IPAddress.NetworkToHostOrder((int)IPAddress.Parse(ip).Address);
}

它给了我这个警告;如果在上面的代码中没有 IPAddress,我会怎么做才能完成同样的事情?

最佳答案

IPAddress.Addressdocumentation 说:



所以我建议你这样做:

public static long CastIp(string ip)
{
    IPAddress address = IPAddress.Parse(ip);
    byte[] addressBytes = address.GetAddressBytes();
    // This restriction is implicit in your existing code, but
    // it would currently just lose data...
    if (addressBytes.Length != 4)
    {
        throw new ArgumentException("Must be an IPv4 address");
    }
    int networkOrder = BitConverter.ToInt32(addressBytes, 0);
    return (uint) IPAddress.NetworkToHostOrder(networkOrder);
}

这似乎给出了相同的结果,在我尝试过的测试中......你应该检查它是否完全符合你的要求,因为它并不清楚你的期望。

关于c# - 警告 CS0618 : 'IPAddress.Address' is obsolete: 'This property has been deprecated,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40317973/

10-13 03:23