次の例外をデバッグしようとしていました。
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 );
私がまだ理解していないのは、それがイミディエイトウィンドウでまったく機能した理由です。