我只是在尝试调试以下异常:
Exception has been thrown by the target of an invocation.: System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. --->
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: newAddress
at System.Net.IPAddress..ctor(Int64 newAddress)
这是相关代码:
int hostToNetworkOrder = IPAddress.HostToNetworkOrder( (int)computer.IpAddress );
IPAddress ipAddress = new IPAddress( hostToNetworkOrder );
computer.IpAddress
被设置为1582281193
。这将被转换为hostToNetworkOrder
,即-374255778
。
现在,如果我尝试在Visual Studio的即时窗口中构造new IPAddress(-374255778);
,我会得到一个包含正确IP地址的IPAddress对象。
但是,如果我越过代码行,它将总是抛出ArgumentOutOfRangeException异常。为什么?
更新
我知道我的输入是负数很可能就是问题的源头。这就是我解决问题的方法:
UInt32 hostToNetworkOrder = (UInt32)IPAddress.HostToNetworkOrder( (Int32)computer.IpAddress );
IPAddress ipAddress = new IPAddress( hostToNetworkOrder );
我还不明白的是为什么在即时窗口中它能够工作。