1

当我发现我不能简单地这样做时,我真的很惊讶:

    Dim ip = IPAddress.Parse("192.168.1.3")
    Dim mask = IPAddress.Parse("255.255.255.0")

    ' next lines won't compile
    Dim networkAddress = ip And mask ' would produce 192.168.1.0
    Dim broadcastAddress = ip Or (Not mask) ' would produce 192.168.1.255

因此,在我自己在子类上实现此类功能之前:我是否遗漏了一些明显的东西?是否有我忽略的未实施的原因?

我承认我在网络课程中可能没有像我应该关注的那样关注。

编辑

我想出了这段代码:

    Dim ip = IPAddress.Parse("192.168.1.3")
    Dim mask = IPAddress.Parse("255.255.255.0")

    Dim ipUnsignedInteger = BitConverter.ToUInt32(ip.GetAddressBytes, 0)
    Dim maskUnsignedInteger = BitConverter.ToUInt32(mask.GetAddressBytes, 0)

    Dim networkAddressUnsignedInteger = ipUnsignedInteger And maskUnsignedInteger
    Dim networkAddress = New IPAddress(networkAddressUnsignedInteger)

    Dim broadcastAddressUnsignedInteger = ipUnsignedInteger Or (Not maskUnsignedInteger)
    Dim broadcastAddress = New IPAddress(broadcastAddressUnsignedInteger)

它可以工作,但是对于我来说似乎如此明显的东西,它的额外代码数量令人难以置信!为什么这不是框架的一部分?!

4

1 回答 1

0
Dim networkaddress2 As New IPAddress(ip.Address And mask.Address)
Dim broadcastAddress2 = New IPAddress(CUInt(ip.Address) Or (Not CUInt(mask.Address)))

但是请注意,不推荐使用 IPAddress.Address。

于 2013-02-15T02:30:14.977 回答