c# 序列化和反序列化 深拷贝

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.InteropServices;

namespace serialize
{
    [Serializable]
    class person
    {
        public string _Name
        { set; get; }
        public string _Gend
        { set; get; }
        public UInt16 Age
        { set; get; }
        public void  sayHello()
        {
            Console.WriteLine("Hello");
        }
    }
     [Serializable]
    class bag
    {
         public string _color
         { set; get; }
    }
    [Serializable]
    class student :person
    {
        //子类要被序列化 其父类也一定要被标记为可序列化
        //所有字段的类型都要是可以被序列化 例如bag
        public bag mybag = new bag();
        public string classid
        { set; get; }
       new public void sayHello()
        {
            Console.WriteLine("I am a student hello");
        }
        //方法不参与序列化。即使方法的形参和返回值类型不要指定标记[serializable]
       public  bag huanbao(bag newbag)
       {
           bag tempbag= mybag;
           mybag = newbag;
           return tempbag;
       }
        //深拷贝
   
    }
    [Serializable]
    class serialiezi
    {
        public int m_is = 0;
        public int ISV
        {
            set;
            get;
        }
        //[Serializable]
        public class subclass1
        {
            [NonSerialized]
            public int ix = 8;
            public int iy = 9;
            public int xys
            { set; get; }
        }
    }

    class Program
    {
        //序列化 目的是将对象 转成成byte[]
        // 1 要在对象前加[serializable] 特征 加以标记
        // 2 子类要被序列化 其父类也一定要被标记为可序列化
        // 3 方法不参与序列化。即使方法的形参和返回值类型不要指定标记[serializable]
        // 4 不需要的序列化的字段(不能是自动属性)标记为[NonSerialized]
        // 5 序列化可以用来做深拷贝
        public static  student DeepCopy(student copyedobj)
        {
            BinaryFormatter bf = new BinaryFormatter();
            //byte[] barr = new byte[5];
            using (MemoryStream ms = new MemoryStream(1024))
            {
                //序列化到内存中
                bf.Serialize(ms, copyedobj);
                //要将内存指针指向开始位置
                ms.Seek(0, 0);
                //反序列化
                object sobj = bf.Deserialize(ms);
                ms.Close();
                return sobj as student;
            }
        }
        public static string  GetMemoryAddr(object o)
        {
            GCHandle h = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection);
            IntPtr addr = GCHandle.ToIntPtr(h);
            return "0x" + addr.ToString("X");
        }
        static void Main(string[] args)
        {
            serialiezi s1 = new serialiezi();
            BinaryFormatter bf = new BinaryFormatter();
            using(FileStream fs = new FileStream(".\\SerilizeFile.bin",FileMode.Create ))
            {
                bf.Serialize(fs,s1);
            }
            student s2 = new student();
            s2._Gend = "男";
            s2._Name = "jack";
            s2.Age = 54;
            s2.classid = "101";
            s2.mybag._color = "卡其色";
            using (FileStream fs = new FileStream(".\\SerilizeFile1.bin", FileMode.Create))
            {
                bf.Serialize(fs, s2);
            }
           student s3= DeepCopy(s2);
           Console.WriteLine("S2 姓名:{0},性别:{1}, 年龄:{2}  班级:{3} 书包颜色:{4} bag 地址:{5} s2地址{6}", s2._Name, s2._Gend, s2.Age, s2.classid, s2.mybag._color, GetMemoryAddr(s2.mybag), GetMemoryAddr(s2));
           Console.WriteLine("S3 姓名:{0},性别:{1}, 年龄:{2}  班级:{3} 书包颜色:{4} bag 地址:{5} s3地址{6}", s3._Name, s3._Gend, s3.Age, s3.classid, s3.mybag._color, GetMemoryAddr(s3.mybag), GetMemoryAddr(s3));
            Console.WriteLine("序列化完成");
            Console.ReadLine();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wuan584974722/article/details/83099657