将嵌套结构转换为字节数组
该代码
using System;
using System.Runtime.InteropServices;
class Program {
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Inner{
public byte firstByte;
public byte secondByte;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Outer{
public short firstShort;
public Inner[] inners;
};
public static byte[] ConvertStructToByteArray(object obj)
{
int len = Marshal.SizeOf(obj);
byte[] arr = new byte[len];
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, len);
Marshal.FreeHGlobal(ptr);
return arr;
}
public static void Main (string[] args) {
Console.WriteLine ("Hello World");
var inner = new Inner();
inner.firstByte = 0x00;
inner.secondByte = 0x01;
var outer = new Outer();
outer.firstShort = 0x0000;
outer.inners = new Inner[]{inner};
var byteArray = ConvertStructToByteArray(outer);
}
}
应该将外部
struct实例转换为字节数组,而是抛出了
Unhandled exception. System.ArgumentException: Type 'Program+Outer' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
at System.Runtime.InteropServices.Marshal.SizeOfHelper(Type t, Boolean throwIfNotMarshalable)
at System.Runtime.InteropServices.Marshal.SizeOf(Object structure)
at Program.ConvertStructToByteArray(Object obj) in /home/runner/ZigzagGracefulDatum/main.cs:line 23
at Program.Main(String[] args) in /home/runner/ZigzagGracefulDatum/main.cs:line 47
为什么大小评估有问题?如何修复?有更好的解决方案吗?
The code
using System;
using System.Runtime.InteropServices;
class Program {
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Inner{
public byte firstByte;
public byte secondByte;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Outer{
public short firstShort;
public Inner[] inners;
};
public static byte[] ConvertStructToByteArray(object obj)
{
int len = Marshal.SizeOf(obj);
byte[] arr = new byte[len];
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, len);
Marshal.FreeHGlobal(ptr);
return arr;
}
public static void Main (string[] args) {
Console.WriteLine ("Hello World");
var inner = new Inner();
inner.firstByte = 0x00;
inner.secondByte = 0x01;
var outer = new Outer();
outer.firstShort = 0x0000;
outer.inners = new Inner[]{inner};
var byteArray = ConvertStructToByteArray(outer);
}
}
is supposed to convert the outer
struct instance into a byte array, but instead throws
Unhandled exception. System.ArgumentException: Type 'Program+Outer' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
at System.Runtime.InteropServices.Marshal.SizeOfHelper(Type t, Boolean throwIfNotMarshalable)
at System.Runtime.InteropServices.Marshal.SizeOf(Object structure)
at Program.ConvertStructToByteArray(Object obj) in /home/runner/ZigzagGracefulDatum/main.cs:line 23
at Program.Main(String[] args) in /home/runner/ZigzagGracefulDatum/main.cs:line 47
Why is there a problem with the size evaluation? How can it be fixed? Is there a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论