首页 > 解决方案 > 来自特定索引的字节数组作为 c# 中的结构而无需复制

问题描述

目前,我编写客户端-服务器垃圾代码,并大量处理通过网络传递的 C++ 结构。我知道这里提供的方法Read a C/C++ data structure in C# from a byte array,但它们都是关于制作副本的。

我想要这样的东西:

struct/*or class*/ SomeStruct
{
    public uint F1;
    public uint F2;
    public uint F3;
}

稍后在我的代码中,我想要这样的东西:

byte[] Data; //16 bytes that I got from network
SomeStruct PartOfDataAsSomeStruct { get { return /*make SomeStruct instance based on this.Data starting from index 4, without copying it. So when I do PartOfDataAsSomeStruct.F1 = 132465; it also changes bytes 4, 5, 6 and 7 in this.Data.*/; } }

如果这是可能的,请告诉如何?

标签: c#arraysstructmarshalling

解决方案


像这样?

byte[] data = new byte[16];
// 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
Console.WriteLine(BitConverter.ToString(data));
ref SomeStruct typed = ref Unsafe.As<byte, SomeStruct>(ref data[4]);
typed.F1 = 42;
typed.F2 = 3;
typed.F3 = 9;
// 00-00-00-00-2A-00-00-00-03-00-00-00-09-00-00-00
Console.WriteLine(BitConverter.ToString(data));

这使用作为数据的“内部托管指针”的 ref-local 从字节数组的中间强制数据。零拷贝。

如果您需要多个项目(例如矢量的工作方式),您可以使用 span 和MemoryMarshal.Cast

请注意,它对元素使用 CPU-endian 规则——在我的例子中是 little endian。

对于跨度:

byte[] data = new byte[256];
// create a span of some of it
var span = new Span<byte>(data, 4, 128);
// now coerce the span
var typed = MemoryMarshal.Cast<byte, SomeStruct>(span);
Console.WriteLine(typed.Length); // 10 of them fit
typed[3].F1 = 3; // etc

推荐阅读