Click here to Skip to main content
12,406,342 members (66,299 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

83.9K views
861 downloads
31 bookmarked
Posted

Deep copy of objects in C#

, 18 Jul 2009 CPOL
How to do a deep copy of objects using System.Reflection.
HCloner
HCloner
bin
Debug
HCloner.dll
Properties
HClonerTest
bin
Debug
HCloner.dll
HClonerTest.dll
nunit.framework.dll
HClonerTest.csproj.user
Properties
TestObjects
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)

Share

About the Author

Hakger
Software Developer (Senior)
Poland Poland
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 18 Jul 2009
Article Copyright 2009 by Hakger
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid