IntPtr.ToInt32() 在 64 位机器上抛出异常,但 IntPtrToInt64() 会在 32 位机器上抛出异常吗?
IntPtr.ToInt32() throws excption on 64bit, but will IntPtrToInt64() throw on 32bit machine?
我有这个代码:
if (hnd == IntPtr.Zero || hnd.ToInt32() == -1)
hnd
是一个 IntPtr
这个抛出OverflowException
,所以我修改为
if (hnd == IntPtr.Zero || hnd.ToInt64() == -1)
文档说 ToInt32 可以抛出异常,但 ToInt64 不能(?)。
//
// Summary:
// Converts the value of this instance to a 32-bit signed integer.
//
// Returns:
// A 32-bit signed integer equal to the value of this instance.
//
// Exceptions:
// T:System.OverflowException:
// On a 64-bit platform, the value of this instance is too large or too small to
// represent as a 32-bit signed integer.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SecuritySafeCritical]
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public int ToInt32();
//
// Summary:
// Converts the value of this instance to a 64-bit signed integer.
//
// Returns:
// A 64-bit signed integer equal to the value of this instance.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SecuritySafeCritical]
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public long ToInt64();
所以问题:
hnd.ToInt64()
是否会在 32 位机器上抛出异常?
documentation 指出:
An instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems
所以:
- 在 64 位 OS 上,IntPtr 可以从
Int64.MinValue
变为 Int64.MaxValue
。显然,转换为 Int32
时可能会引发溢出,因为范围更长。
- 在 32 位 OS 上,IntPtr 可以从
Int32.MinValue
变为 Int32.MaxValue
,因此您可以将其转换为 Int64
和 Int32
,因为该值将始终在范围内。
我有这个代码:
if (hnd == IntPtr.Zero || hnd.ToInt32() == -1)
hnd
是一个 IntPtr
这个抛出OverflowException
,所以我修改为
if (hnd == IntPtr.Zero || hnd.ToInt64() == -1)
文档说 ToInt32 可以抛出异常,但 ToInt64 不能(?)。
//
// Summary:
// Converts the value of this instance to a 32-bit signed integer.
//
// Returns:
// A 32-bit signed integer equal to the value of this instance.
//
// Exceptions:
// T:System.OverflowException:
// On a 64-bit platform, the value of this instance is too large or too small to
// represent as a 32-bit signed integer.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SecuritySafeCritical]
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public int ToInt32();
//
// Summary:
// Converts the value of this instance to a 64-bit signed integer.
//
// Returns:
// A 64-bit signed integer equal to the value of this instance.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SecuritySafeCritical]
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public long ToInt64();
所以问题:
hnd.ToInt64()
是否会在 32 位机器上抛出异常?
documentation 指出:
An instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems
所以:
- 在 64 位 OS 上,IntPtr 可以从
Int64.MinValue
变为Int64.MaxValue
。显然,转换为Int32
时可能会引发溢出,因为范围更长。 - 在 32 位 OS 上,IntPtr 可以从
Int32.MinValue
变为Int32.MaxValue
,因此您可以将其转换为Int64
和Int32
,因为该值将始终在范围内。