示例#1
0
        public static void Run()
        {
            School school = new School()
            {
                Name    = "秀尽学院",
                Number  = 0,
                Student = new Student()
                {
                    Name = "Joker",
                    Age  = 18
                }
            };

            Console.WriteLine("--------原始对象");
            ShowSchoolInfo(school);

            Console.WriteLine("\n--------深拷贝方法1:序列化");
            School serSchool = HelperTools.SerializableClone(school);

            serSchool.Name         = "序列化  ";
            serSchool.Number       = 1;
            serSchool.Student.Name = "Skull";
            serSchool.Student.Age  = 20;
            ShowSchoolInfo(serSchool);
            ShowSchoolInfo(school);

            Console.WriteLine("\n--------深拷贝方法2:新建对象");
            School newSchool = (School)school.NewClone();

            newSchool.Name         = "new对象 ";
            newSchool.Number       = 2;
            newSchool.Student.Name = "Panther";
            newSchool.Student.Age  = 22;
            ShowSchoolInfo(newSchool);
            ShowSchoolInfo(school);


            Console.WriteLine("\n--------浅拷贝方法1:属性反射");
            School proSchool = HelperTools.PropertyClone(school);

            proSchool.Name         = "反射    ";
            proSchool.Number       = 3;
            proSchool.Student.Name = "Fox";
            proSchool.Student.Age  = 21;
            ShowSchoolInfo(proSchool);
            ShowSchoolInfo(school);

            Console.WriteLine("\n--------浅拷贝2:克隆接口");
            School cloneSchool = (School)school.Clone();

            cloneSchool.Name         = "克隆    ";
            cloneSchool.Number       = 4;
            cloneSchool.Student.Name = "Queen";
            cloneSchool.Student.Age  = 23;
            ShowSchoolInfo(cloneSchool);
            ShowSchoolInfo(school);

            Console.ReadLine();
        }