C# 结构体直接赋值的问题

来源:互联网 发布:钢铁力量天蝎数据 编辑:程序博客网 时间:2024/05/17 10:52


hehe =DeepClone(haha);

 public static T DeepClone<T>(T obj)
        {
            using (var ms = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(ms, obj);    //obj 定义需要加上 [Serializable]  ybhjx
                ms.Position = 0;

                return (T)formatter.Deserialize(ms);
            }
        }


 public object memcpy( object des_obj1, object src_obj2)
       {
           byte[] aaa = StructToBytes(des_obj1);
           IntPtr hPtr = Marshal.UnsafeAddrOfPinnedArrayElement(aaa, 0);//获取字节数组首地址
           byte[] bbb = StructToBytes(src_obj2);
           Marshal.Copy(bbb, 0, hPtr, bbb.Length);
           des_obj1 = BytesToStuct(aaa, src_obj2.GetType());
           return des_obj1;
       }

0 0