是否可以安全地假设仅在 C# 中序列化和仅在 C# 中反序列化的int
不会受到体系结构字节序的影响,因为BitConverter.IsLittleEndian
是硬编码的true
C#可以编译为在dotnetcore下运行。在这里,您认为IsLittleEndian
被硬编码为true
的假设是不正确的。它在编译时被硬编码为一个值,但这可能是true
或false
,具体取决于编译目标。
见 https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/BitConverter.cs#L21
#if BIGENDIAN
[Intrinsic]
public static readonly bool IsLittleEndian /* = false */;
#else
[Intrinsic]
public static readonly bool IsLittleEndian = true;
#endif
显然,虽然.NET/dotnet核心没有,但Mono可以针对一些大端架构。