在 C# 中序列化 在 C# BitConverter 中反序列化,并保证字节序



是否可以安全地假设仅在 C# 中序列化和仅在 C# 中反序列化的int不会受到体系结构字节序的影响,因为BitConverter.IsLittleEndian是硬编码的true

C#可以编译为在dotnetcore下运行。在这里,您认为IsLittleEndian被硬编码为true的假设是不正确的。它在编译时被硬编码为一个值,但这可能是truefalse,具体取决于编译目标。

见 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可以针对一些大端架构。

最新更新