对象序列化补充

本文介绍了一个C#中的数据结构BaseCommunicationTypeA及其嵌套结构BaseNestedParamE和BaseNestedParamF的序列化与反序列化实现过程。文章详细展示了如何通过Unsafe代码块和Marshal类来处理不同类型的数据,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace DataBaseClassCollection
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 4)]
    public class BaseCommunicationTypeA
    {
        public int operation_id;
        public int param_a;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
        public string param_d;
        public int nested_param_e_array_size;
        public BaseNestedParamE[] nested_param_e_array;
        public int nested_param_f_array_size;
        public BaseNestedParamF[] nested_param_f_array;
        public double other_param;

        public byte[] Serialize()
        {
            try
            {
                #region 计算长度
                int TotalLength = 0;
                int nested_param_e_array_index = 0;
                int nested_param_f_array_index = 0;
                TotalLength += Marshal.SizeOf(typeof(int));  //operation_id
                TotalLength += Marshal.SizeOf(typeof(int));  //param_a
                TotalLength += 20;  //param_d
                TotalLength += Marshal.SizeOf(typeof(int));  //nested_param_e_array_size
                TotalLength += Marshal.SizeOf(typeof(int));  //nested_param_e_array的指针
                TotalLength += Marshal.SizeOf(typeof(int));  //nested_param_f_array_size
                TotalLength += Marshal.SizeOf(typeof(int));  //nested_param_f_array的指针
                TotalLength += Marshal.SizeOf(typeof(double));  //other_param
                nested_param_e_array_index = TotalLength;
                TotalLength += Marshal.SizeOf(typeof(BaseNestedParamE)) * nested_param_e_array_size;  //nested_param_e_array
                nested_param_f_array_index = TotalLength;
                TotalLength += Marshal.SizeOf(typeof(BaseNestedParamF)) * nested_param_f_array_size;  //nested_param_f_array
                #endregion
                #region 序列化
                byte[] data = new byte[TotalLength];
                int copyindex = 0;
                unsafe
                {
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //operation_id
                    {
                        Marshal.StructureToPtr(operation_id, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(int)));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //param_a
                    {
                        Marshal.StructureToPtr(param_a, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(int)));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    //param_d
                    byte[] param_d_data = Encoding.Unicode.GetBytes(param_d);
                    int param_d_data_length = param_d_data.Length > 20 ? 20 : param_d_data.Length;
                    Array.Copy(param_d_data, 0, data, copyindex, param_d_data_length);
                    param_d_data = null;
                    copyindex += 20;
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //nested_param_e_array_size
                    {
                        Marshal.StructureToPtr(nested_param_e_array_size, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(int)));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //nested_param_e_array的指针
                    {
                        Marshal.StructureToPtr(nested_param_e_array_index, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(int)));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //nested_param_f_array_size
                    {
                        Marshal.StructureToPtr(nested_param_f_array_size, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(int)));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //nested_param_f_array的指针
                    {
                        Marshal.StructureToPtr(nested_param_f_array_index, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(int)));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(double))])  //other_param
                    {
                        Marshal.StructureToPtr(other_param, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(double)));
                        copyindex += Marshal.SizeOf(typeof(double));
                    }
                    for (int i = 0; i < nested_param_e_array_size; i++)
                    {
                        Array.Copy(nested_param_e_array[i].Serialize(), 0, data, copyindex, Marshal.SizeOf(typeof(BaseNestedParamE)));
                        copyindex += Marshal.SizeOf(typeof(BaseNestedParamE));
                    }
                    for (int i = 0; i < nested_param_f_array_size; i++)
                    {
                        Array.Copy(nested_param_f_array[i].Serialize(), 0, data, copyindex, Marshal.SizeOf(typeof(BaseNestedParamF)));
                        copyindex += Marshal.SizeOf(typeof(BaseNestedParamF));
                    }
                    GC.Collect();
                }
                #endregion
                return data;
            }
            catch (Exception ex)
            {
                throw new Exception("BaseCommunicationTypeA序列化失败", ex);
            }
        }
        public void DeSerialize(byte[] Data)
        {
            try
            {
                #region 反序列化
                int copyindex = 0;
                unsafe
                {
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //operation_id
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(int)));
                        operation_id = (int)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(int));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //param_a
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(int)));
                        param_a = (int)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(int));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    //param_d
                    byte[] param_d_data = new byte[20];
                    Array.Copy(Data, copyindex, param_d_data, 0, 20);
                    param_d = Encoding.Unicode.GetString(param_d_data);
                    param_d_data = null;
                    copyindex += 20;
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //nested_param_e_array_size
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(int)));
                        nested_param_e_array_size = (int)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(int));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    int nested_param_e_array_index = 0;
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //nested_param_e_array的指针
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(int)));
                        nested_param_e_array_index = (int)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(int));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //nested_param_f_array_size
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(int)));
                        nested_param_f_array_size = (int)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(int));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    int nested_param_f_array_index = 0;
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //nested_param_f_array的指针
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(int)));
                        nested_param_f_array_index = (int)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(int));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(double))])  //other_param
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(double)));
                        other_param = (double)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(double));
                        copyindex += Marshal.SizeOf(typeof(double));
                    }
                    //nested_param_e_array
                    nested_param_e_array = new BaseNestedParamE[nested_param_e_array_size];
                    byte[] nested_param_e_array_data = new byte[Marshal.SizeOf(typeof(BaseNestedParamE))];
                    for (int i = 0; i < nested_param_e_array_size; i++)
                    {
                        Array.Clear(nested_param_e_array_data, 0, nested_param_e_array_data.Length);
                        nested_param_e_array[i] = new BaseNestedParamE();
                        Array.Copy(Data, copyindex, nested_param_e_array_data, 0, Marshal.SizeOf(typeof(BaseNestedParamE)));
                        nested_param_e_array[i].DeSerialize(nested_param_e_array_data);
                        copyindex += Marshal.SizeOf(typeof(BaseNestedParamE));
                    }
                    nested_param_e_array_data = null;
                    //nested_param_f_array
                    nested_param_f_array = new BaseNestedParamF[nested_param_f_array_size];
                    byte[] nested_param_f_array_data = new byte[Marshal.SizeOf(typeof(BaseNestedParamF))];
                    for (int i = 0; i < nested_param_f_array_size; i++)
                    {
                        Array.Clear(nested_param_f_array_data, 0, nested_param_f_array_data.Length);
                        nested_param_f_array[i] = new BaseNestedParamF();
                        Array.Copy(Data, copyindex, nested_param_f_array_data, 0, Marshal.SizeOf(typeof(BaseNestedParamF)));
                        nested_param_f_array[i].DeSerialize(nested_param_f_array_data);
                        copyindex += Marshal.SizeOf(typeof(BaseNestedParamF));
                    }
                    nested_param_f_array_data = null;
                }
                GC.Collect();
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception("BaseCommunicationTypeA反序列化失败", ex);
            }
        }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 4)]
    public class BaseNestedParamE
    {
        public int param_a;
        public double param_b;
        public int param_c;

        public byte[] Serialize()
        {
            try
            {
                #region 计算长度
                int TotalLength = 0;
                TotalLength += Marshal.SizeOf(typeof(int));  //param_a
                TotalLength += Marshal.SizeOf(typeof(double));  //param_b
                TotalLength += Marshal.SizeOf(typeof(int)); //param_c
                #endregion
                #region 序列化
                byte[] data = new byte[TotalLength];
                int copyindex = 0;
                unsafe
                {
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //param_a
                    {
                        Marshal.StructureToPtr(param_a, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(int)));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(double))])  //param_b
                    {
                        Marshal.StructureToPtr(param_b, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(double)));
                        copyindex += Marshal.SizeOf(typeof(double));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //param_c
                    {
                        Marshal.StructureToPtr(param_c, (IntPtr)pobjdata, true);
                        Marshal.Copy((IntPtr)pobjdata, data, copyindex, Marshal.SizeOf(typeof(int)));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    GC.Collect();
                }
                #endregion
                return data;
                #region 序列化old
                //byte[] data = new byte[Marshal.SizeOf(typeof(BaseNestedParamE))];
                //unsafe
                //{
                //    fixed (byte* pobjdata = new byte[data.Length])
                //    {
                //        Marshal.StructureToPtr(this, (IntPtr)pobjdata, true);
                //        Marshal.Copy((IntPtr)pobjdata, data, 0, data.Length);
                //        return data;
                //    }
                //}
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception("BaseNestedParamE序列化失败", ex);
            }
        }
        public void DeSerialize(byte[] Data)
        {
            try
            {
                #region 反序列化
                int copyindex = 0;
                unsafe
                {
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //param_a
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(int)));
                        param_a = (int)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(int));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(double))])  //param_b
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(double)));
                        param_b = (double)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(double));
                        copyindex += Marshal.SizeOf(typeof(double));
                    }
                    fixed (byte* pobjdata = new byte[Marshal.SizeOf(typeof(int))])  //param_c
                    {
                        Marshal.Copy(Data, copyindex, (IntPtr)pobjdata, Marshal.SizeOf(typeof(int)));
                        param_c = (int)Marshal.PtrToStructure((IntPtr)pobjdata, typeof(int));
                        copyindex += Marshal.SizeOf(typeof(int));
                    }
                }
                GC.Collect();
                #endregion

                #region 反序列化old
                //unsafe
                //{
                //    fixed (byte* pContext = Data)
                //    {
                //        BaseNestedParamE info = (BaseNestedParamE)Marshal.PtrToStructure((IntPtr)pContext, typeof(BaseNestedParamE));
                //        param_a = info.param_a;
                //        param_b = info.param_b;
                //        param_c = info.param_c;
                //    }
                //}
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception("BaseNestedParamE反序列化失败", ex);
            }
        }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 4)]
    public class BaseNestedParamF
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 512)]
        public string param_b;

        public byte[] Serialize()
        {
            try
            {                
                #region 计算长度
                int TotalLength = 0;
                TotalLength += 512;  //param_b
                #endregion
                #region 序列化
                byte[] data = new byte[TotalLength];
                int copyindex = 0;
                unsafe
                {
                    //param_b
                    byte[] param_b_data = Encoding.Unicode.GetBytes(param_b);
                    int param_b_data_length = param_b_data.Length > 512 ? 512 : param_b_data.Length;
                    Array.Copy(param_b_data, 0, data, copyindex, param_b_data_length);
                    param_b_data = null;
                    copyindex += 512;
                    GC.Collect();
                }
                #endregion
                return data;
                #region 序列化old
                //byte[] data = new byte[Marshal.SizeOf(typeof(BaseNestedParamF))];
                //unsafe
                //{
                //    fixed (byte* pobjdata = new byte[data.Length])
                //    {
                //        Marshal.StructureToPtr(this, (IntPtr)pobjdata, true);
                //        Marshal.Copy((IntPtr)pobjdata, data, 0, data.Length);
                //        return data;
                //    }
                //}
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception("BaseNestedParamF序列化失败", ex);
            }
        }
        public void DeSerialize(byte[] Data)
        {
            try
            {
                #region 反序列化
                int copyindex = 0;
                unsafe
                {
                    //param_b
                    byte[] param_b_data = new byte[512];
                    Array.Copy(Data, copyindex, param_b_data, 0, 512);
                    param_b = Encoding.Unicode.GetString(param_b_data);
                    param_b_data = null;
                    copyindex += 512;
                }
                GC.Collect();
                #endregion

                #region 反序列化old
                //unsafe
                //{
                //    fixed (byte* pContext = Data)
                //    {
                //        BaseNestedParamF info = (BaseNestedParamF)Marshal.PtrToStructure((IntPtr)pContext, typeof(BaseNestedParamF));
                //        param_a = info.param_a;
                //        param_b = info.param_b;
                //    }
                //}
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception("BaseNestedParamF反序列化失败", ex);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值