我希望能够通过包含已知结构的 TCP/IP 接收一些二进制数据。我不想与 C 或 C++ 进行互操作,因此适用于这种情况的解决方案对我没有帮助。不幸的是,对方无法更改协议。当我尝试读取具有给定格式的二进制文件时,也会出现问题。
我也检查BinaryFormatter
过和类似的,但他们使用自己的格式,这对我来说是不可接受的。
这是一组示例结构。我希望能够重建结构的嵌套数组(已知长度)。使用当前代码,我得到一个异常:
无法从程序集“...”加载类型“NestedStruct”,因为它包含偏移量 2 处的对象字段,该对象字段未正确对齐或被非对象字段重叠。
我希望能够发送/接收(或读/写)struct MainStruct
.
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 244, CharSet = CharSet.Ansi)]
public struct NestedStruct
{
[FieldOffset(0)]
public Int16 someInt;
[FieldOffset(2), MarshalAs(UnmanagedType.ByValArray, SizeConst = 242)]
public Byte[] characterArray; // an array of fixed length 242
}
[StructLayout(LayoutKind.Explicit)]
public struct OtherNestedStruct
{
[FieldOffset(0)]
public Int16 someInt;
[FieldOffset(2)]
public Int16 someOtherInt;
}
[StructLayout(LayoutKind.Explicit)]
public struct MainStruct
{
[FieldOffset(0)]
public double someDouble;
[FieldOffset(8)]
public NestedStruct nestedContent;
[FieldOffset(8 + 244)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 13 * 4)]
public OtherNestedStruct[] arrayOfStruct; // fixed array length 13
}
更新:
这是我的最新版本:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct NestedStruct
{
public Int16 someInt;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 242)]
public Byte[] characterArray; // an array of fixed length 242
}
[StructLayout(LayoutKind.Sequential , Pack=1)]
public struct OtherNestedStruct
{
public Int16 someInt;
public Int16 someOtherInt;
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct MainStruct
{
public double someDouble;
public NestedStruct nestedContent;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 13)]
public OtherNestedStruct[] arrayOfStruct; // fixed array length 13
}