C# 结构体数组如何保存到文件并从文件解析出来

C# 结构体数组如何保存到文件并从文件解析出来
  public struct  USER_RECORD
        {
            public uint nGroupID;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public byte[] szUserID;
            public uint nFinger;
            public uint nInstances;
            public VCOMWrapper.MX00_TEMPLATE_INSTANCE[] instanceRecords;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct MX00_TEMPLATE_INSTANCE
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
            public byte[] p378Template;
        }



List<USER_RECORD> ps = new List<USER_RECORD>();

现在要把ps保存成文件,并且下次文件能正确解析出ps?

如果结构体USER_RECORD没有instanceRecords,就可以把这个结构体转成Byte[], 然后写到文件,解析时,读取文件byte[]转成结构体,可是现在有VCOMWrapper.MX00_TEMPLATE_INSTANCE[], 结构体转成Byte[]失败.

------解决思路----------------------
序列化比较简单写
http://www.cnblogs.com/yzenet/p/3622833.html
------解决思路----------------------

        public static void Serialize(USER_RECORD rec, string path)         {            using (BinaryWriter wr = new BinaryWriter(File.OpenWrite(path)))             {                 Serialize(rec, wr);             }         }         public static USER_RECORD Deserialize(string path)         {             using (BinaryReader rd = new BinaryReader(File.OpenRead(path)))             {                 return Deserialize(rd);             }         }         private static void Serialize(USER_RECORD rec, BinaryWriter output)         {             output.Write(rec.nGroupID);             WriteByteArray(rec.szUserID, output);             output.Write(rec.nFinger);             output.Write(rec.nInstances);             if (rec.nInstances > 0)             {                 MX00_TEMPLATE_INSTANCE[] array = rec.instanceRecords;                 Debug.Assert(array != null && array.Length == rec.nInstances);                 for (int i = 0; i < rec.nInstances; i++)                 {                     MX00_TEMPLATE_INSTANCE inst = array[i];                     WriteByteArray(inst.p378Template, output);                 }             }         }         private static void WriteByteArray(byte[] data, BinaryWriter output)         {             if (data != null)             {                 output.Write(data.Length);                 output.Write(data);             }             else                 output.Write(-1);         }         private static byte[] ReadByteArray(BinaryReader input)         {             int len = input.ReadInt32();             if (len >= 0)             {                if (len > 0)                     return input.ReadBytes(len);                 else                     return new byte[0];             }             else                return null;         }         private static USER_RECORD Deserialize(BinaryReader input)         {             USER_RECORD rec = new USER_RECORD();             int len;             rec.nGroupID = input.ReadUInt32();             rec.szUserID = ReadByteArray(input);             rec.nFinger = input.ReadUInt32();             len = (int)(rec.nInstances = input.ReadUInt32());             MX00_TEMPLATE_INSTANCE[] array = new MX00_TEMPLATE_INSTANCE[len];             for (int i = 0; i < len; i++)             {                 MX00_TEMPLATE_INSTANCE inst = new MX00_TEMPLATE_INSTANCE();                 inst.p378Template = ReadByteArray(input);                 array[i] = inst;             }           rec.instanceRecords = array;
  return rec;
        }
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值