新手请教一下C#下的结构体嵌套的问题
需要通过串口通信,对方是C++写的程序,通信结构体为[StructLayout(LayoutKind.Sequential)]
public struct BT_BLADE_INFO
{
public byte state;
public byte reserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] cpu_tmp;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] bld_ip;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] bld_netmask;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] bld_gateway;
}
[StructLayout(LayoutKind.Sequential)]
public struct BT_PACKAGE_DWON
{
public byte head;
public byte package_type;
public short verify_value;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public BT_BLADE_INFO[] blade_info;
}
现在需要把这个结构体转换为byte流进行通信,程序如下:
BT_PACKAGE_DWON down_package = new BT_PACKAGE_DWON();
int aaa = Marshal.SizeOf(down_package);
现在总是报错
A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll
用Marshal.StructureToPtr也是一样的错误,我的程序是在wince5.0下执行,哪位知道这是为什么?或者说这个嵌套结构体该如何实现?
谢谢!