【发布时间】:2010-09-15 10:38:51
【问题描述】:
我有一个字节数组,我想将它重新解释为一个 blittable 结构数组,最好不要复制。使用不安全的代码很好。我知道字节数,以及我想在最后得到的结构数。
public struct MyStruct
{
public uint val1;
public uint val2;
// yadda yadda yadda....
}
byte[] structBytes = reader.ReadBytes(byteNum);
MyStruct[] structs;
fixed (byte* bytes = structBytes)
{
structs = // .. what goes here?
// the following doesn't work, presumably because
// it doesnt know how many MyStructs there are...:
// structs = (MyStruct[])bytes;
}
【问题讨论】:
-
我相信您可以在 stackoverflow.com/questions/621493/… 找到您的答案,其中包含适用于您的情况的转换技术。
标签: c# struct bytearray unsafe