可以用 Serializable 类进行串行化.

解决方案 »

  1.   

    既然是struct,也可以用指针实现——但是最好为struct指定StructLayout属性。
      

  2.   

    这个问题有两个关键点:
    1.如何将STRUCT转换为字节数组;
    2.如何指定平台以1为单位分配空间而不是以默认大小。1.如何将STRUCT转换为字节数组;
    public struct YourStruct
    { // only value-types possible here:
    public int First;
    public long Second;
    public double Third;
    }static unsafe byte[] YourStructToBytes( YourStruct s )
    {
    byte[] arr = new byte[ sizeof(YourStruct) ];
    fixed( byte* parr = arr )
    { *((YourStruct*)parr) = s; }
    return arr;
    }static unsafe YourStruct BytesToYourStruct( byte[] arr )
    {
    if( arr.Length < sizeof(YourStruct) )
    throw new ArgumentException();YourStruct s;
    fixed( byte* parr = arr )
    { s = *((YourStruct*)parr); }
    return s;
    }// usage:
    YourStruct s0;
    s0.First = 1;
    s0.Second = 2;
    s0.Third = 3.5;
    byte[] ab = YourStructToBytes( s0 );
    // reverse:
    YourStruct s1 = BytesToYourStruct( ab );
    2.如何指定平台以1为单位对齐内存空间而不是以默认大小。
    使用StructLayoutAttribute类
    a.using System.Runtime.InteropServices;
    b.在需要指定对齐方式的STRUCT前加入
    [StructLayout(LayoutKind.Sequential, Pack=1)]