将字节数组转换为对象

Mar*_*ark 1 c#

在C#中有一种方法可以将普通字节数组转换为对象吗?

例如,给这个班:

class Data
{
    public int _int1;
    public int _int2;
    public short _short1;
    public long _long1;
}
Run Code Online (Sandbox Code Playgroud)

我想基本上能够做这样的事情:

var bytes = new byte[] { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0 };
var obj = (Data)bytes;
Run Code Online (Sandbox Code Playgroud)

Mat*_*son 6

您可以尝试编组:

将类的布局声明为Sequential(并注意您将需要使用Pack = 1):

[StructLayout(LayoutKind.Sequential, Pack = 1)]
class Data
{
    public int _int1;
    public int _int2;
    public short _short1;
    public long _long1;
}
Run Code Online (Sandbox Code Playgroud)

将字节编组为Data类的新实例:

var bytes = new byte[] { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0 };
GCHandle gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
var data = (Data)Marshal.PtrToStructure(gcHandle.AddrOfPinnedObject(), typeof(Data));
gcHandle.Free();

// Now data should contain the correct values.

Console.WriteLine(data._int1);    // Prints 1
Console.WriteLine(data._int2);    // Prints 2
Console.WriteLine(data._short1);  // Prints 3
Console.WriteLine(data._long1);   // Prints 4
Run Code Online (Sandbox Code Playgroud)

为方便起见,您可以在Data上编写静态方法来进行转换:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
class Data
{
    public int _int1;
    public int _int2;
    public short _short1;
    public long _long1;

    public static Data FromBytes(byte[] bytes)
    {
        GCHandle gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
        var data = (Data)Marshal.PtrToStructure(gcHandle.AddrOfPinnedObject(), typeof(Data));
        gcHandle.Free();
        return data;
    }
}

...

var data = Data.FromBytes(new byte[] {1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0});
Run Code Online (Sandbox Code Playgroud)

如果您真的想要,可以编写一个显式运算符来转换字节数组,以获取OP中的语法.我建议只使用Data.FromBytes()IMO会更加清晰.

仍然,只是为了完整性:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
class Data
{
    public int _int1;
    public int _int2;
    public short _short1;
    public long _long1;

    public static explicit operator Data(byte[] bytes)
    { 
        GCHandle gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
        var data = (Data)Marshal.PtrToStructure(gcHandle.AddrOfPinnedObject(), typeof(Data));
        gcHandle.Free();
        return data;
    }
}

...

var data = (Data)new byte[] {1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0};
Run Code Online (Sandbox Code Playgroud)