引用类型赋值的时候老赋值引用,大侠们都是怎么深度拷贝的?

解决方案 »

  1.   

    引用类型如果自己写的就自己实现ICloneable接口,每个对象调用Clone复制,反之只能手工复制,一个一个属性的复制。
      

  2.   

    写个复制构造函数用就OK了.
    实现ICloneable接口估计成吧,麻烦.
      

  3.   

    序列化,再反序列化 可行
    不过需要注意的是不能Mark[NonSerialized] attribute,否则反序列化回来会挂。实现ICloneable接口是最好的。
      

  4.   

    class Person
    {
        private string name;
        private int age;    // Copy constructor.
        public Person(Person previousPerson)
        {
            name = previousPerson.name;
            age = previousPerson.age;
        }    //// Alternate copy contructor calls the instance constructor.
        //public Person(Person previousPerson)
        //    : this(previousPerson.name, previousPerson.age)
        //{
        //}    // Instance constructor.
        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }    // Get accessor.
        public string Details
        {
            get
            {
                return name + " is " + age.ToString();
            }
        }
    }class TestPerson
    {
        static void Main()
        {
            // Create a new person object.
            Person person1 = new Person("George", 40);        // Create another new object, copying person1.
            Person person2 = new Person(person1);
            Console.WriteLine(person2.Details);        // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    // Output: George is 40