Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

Deep copy of objects in C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (15 votes)
18 Jul 2009CPOL 153.8K   1.2K   34  
How to do a deep copy of objects using System.Reflection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HAKGERSoft;
using NUnit.Framework;

namespace HAKGERSoft {

    [TestFixture]
    public class ManualTest {

        [Test,ExpectedException(typeof(ArgumentNullException))]
        public void NullFail(){
            HCloner.DeepCopy<List<int>>(null);
        }
        
        [Test]
        public void ValueType(){
            Assert.AreEqual(3,HCloner.DeepCopy(3));
            Assert.AreEqual(3,HCloner.DeepCopy(3));
            Assert.AreNotEqual(3,HCloner.DeepCopy(4));
            Assert.AreNotEqual(3,HCloner.DeepCopy(4));
            Assert.AreEqual(true,HCloner.DeepCopy(true));
            Assert.AreNotEqual(true,HCloner.DeepCopy(false));
            
        }

        [Test]
        public void String(){
            Assert.AreEqual("abba",HCloner.DeepCopy("abba"));
            Assert.AreNotEqual("abba",HCloner.DeepCopy("abbb"));
        }

        [Test]
        public void ManualSimpleObject() {
            SimpleTestObject sto=new SimpleTestObject();
            SimpleTestObject copy=HCloner.DeepCopy(sto);
            Assert.IsFalse(object.ReferenceEquals(sto,copy));
            Assert.IsFalse(object.ReferenceEquals(sto.StrBuilder,copy.StrBuilder));
            Assert.AreEqual(sto.PubInt,copy.PubInt);
            Assert.AreEqual(sto.StrProp,copy.StrProp);
            Assert.AreEqual(sto.StrProp2,copy.StrProp2);

        }

        [Test]
        public void AutomaticProperyValueTyped() {
            AutomaticPropsTestObject apto=new AutomaticPropsTestObject();
            AutomaticPropsTestObject copy=HCloner.DeepCopy(apto);
            //null
            Assert.IsFalse(object.ReferenceEquals(apto,copy));
            Assert.AreEqual(apto.SB,null);
            Assert.AreEqual(copy.SB,null);
            Assert.AreEqual(apto.Integer,copy.Integer);
            Assert.AreEqual(apto.Str,copy.Str);

            //set values
            apto.SB=new StringBuilder();
            apto.Integer=-5;
            apto.Str="abba";

            AutomaticPropsTestObject copy2=HCloner.DeepCopy(apto);
            Assert.IsFalse(object.ReferenceEquals(apto,copy2));
            Assert.IsFalse(object.ReferenceEquals(apto.SB,copy2.SB));
            Assert.AreEqual(copy2.Integer,-5);
            Assert.AreEqual(copy2.Str,"abba");

        }

        [Test]
        public void ArrayValueTyped() {
            int[] array=new int[] { 2,4,9 };
            int[] copy=HCloner.DeepCopy(array);
            Assert.IsFalse(object.ReferenceEquals(array,copy));
            Assert.AreEqual(copy.Length,3);
            Assert.AreEqual(copy[0],2);
            Assert.AreEqual(copy[1],4);
            Assert.AreEqual(copy[2],9);
        }

        [Test]
        public void ArrayRefTyped() {
            StringBuilder[] array=new StringBuilder[] { new StringBuilder(),new StringBuilder() };
            StringBuilder[] copy=HCloner.DeepCopy(array);
            Assert.IsFalse(object.ReferenceEquals(array,copy));
            Assert.AreEqual(copy.Length,2);

            Assert.IsTrue(copy[0]!=null);
            Assert.IsFalse(object.ReferenceEquals(array[0],copy[0]));
            Assert.IsFalse(object.ReferenceEquals(array[1],copy[1]));
        }

        [Test]
        public void ArrayEmpty() {
            int[] array=new int[] {  };
            int[] copy=HCloner.DeepCopy(array);
            Assert.IsFalse(object.ReferenceEquals(array,copy));
            Assert.AreEqual(copy.Length,0);
        }

        [Test]
        public void ListEmpty() {
            List<int> list=new List<int>();
            List<int> copy=HCloner.DeepCopy(list);
            Assert.IsFalse(object.ReferenceEquals(list,copy));
            Assert.AreEqual(copy.Count,0);
        }

        [Test]
        public void ListValueTyped() {
            List<int> list=new List<int> { 2,4,9 };
            List<int> copy=HCloner.DeepCopy(list);
            Assert.IsFalse(object.ReferenceEquals(list,copy));
            Assert.AreEqual(copy.Count,3);
            Assert.AreEqual(copy[0],2);
            Assert.AreEqual(copy[1],4);
            Assert.AreEqual(copy[2],9);
        }

        [Test]
        public void ListRefTyped() {
            List<StringBuilder> list=new List<StringBuilder> { new StringBuilder(),new StringBuilder() };
            List<StringBuilder> copy=HCloner.DeepCopy(list);
            Assert.IsFalse(object.ReferenceEquals(list,copy));
            Assert.AreEqual(copy.Count,2);

            Assert.IsTrue(copy[0]!=null);
            Assert.IsFalse(object.ReferenceEquals(list[0],copy[0]));
            Assert.IsFalse(object.ReferenceEquals(list[1],copy[1]));
        }

        [Test]
        public void Nullable() {
            int? nullint=null;
            //int? copy=HCloner.DeepCopy(nullint);
            //Assert.AreEqual(copy,null);
            nullint=4;
            int? copy2=HCloner.DeepCopy(nullint);
            Assert.AreEqual(copy2.Value,4);
            
        }





    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions