C# 深复制

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

namespace ConsoleApplication13
{
    public class Content
    {
        public int Val2;
    }

    public class Cloner:ICloneable
    {
        public int Val;
        public Content Mycontent = new Content();
        public Cloner(int newVal)
       {
           Mycontent.Val2 = newVal;
           Val=newVal;  
       }
        public object Clone()
        {
            Cloner clonedCloner = new Cloner(Mycontent.Val2);
             return clonedCloner;
        }
    }
    class Tester
    {
        static void Main(string[] args)
        {
            Cloner source = new Cloner(5);
            Cloner target = (Cloner)source.Clone();
            Console.WriteLine("{0},{1}",target.Val,target.Mycontent.Val2);
            source.Mycontent.Val2 = 8;
            Console.WriteLine("{0},{1}", target.Val, target.Mycontent.Val2);
            Console.ReadKey();
        }
    }

}

 

posted on 2013-09-06 22:55  武胜-阿伟  阅读(202)  评论(0编辑  收藏  举报