public void TestCloneMembers() { TestObject[] copies = new TestObject[3]; TestObject objB, objA = TestObject.Create(); using (ObjectCloner cloner = new MemberwiseClone()) { objB = cloner.Clone(objA); for (int i = 0; i < copies.Length; i++) copies[i] = cloner.Clone(objB); } AssertDifferences(objA, objB); //not using serialization yields new instance of singleton Assert.IsFalse(Object.ReferenceEquals(objA.singletonData, objB.singletonData)); //not using serializaiton skips all custom routines Assert.IsFalse(objA.customData.CustomData); Assert.IsFalse(objA.customData.Deserialized); Assert.IsFalse(objB.customData.CustomData); Assert.IsFalse(objB.customData.Deserialized); }
public void TestClonePerf() { const int Reps = 1000; TestObject test = TestObject.Create(); object result; Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 0; i < Reps; i++) { BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream m = new MemoryStream()) { bf.Serialize(m, test); m.Position = 0; result = bf.Deserialize(m); } } timer.Stop(); Console.WriteLine("Elapsed: {0,10} (100%)", timer.ElapsedTicks); long percentof = timer.ElapsedTicks; timer = new Stopwatch(); timer.Start(); for (int i = 0; i < Reps; i++) { result = new SerializerClone().Clone(test); } timer.Stop(); Console.WriteLine("Elapsed: {0,10} ({1}%)", timer.ElapsedTicks, (timer.ElapsedTicks * 100) / percentof); timer = new Stopwatch(); timer.Start(); for (int i = 0; i < Reps; i++) { result = new MemberwiseClone().Clone(test); } timer.Stop(); Console.WriteLine("Elapsed: {0,10} ({1}%)", timer.ElapsedTicks, (timer.ElapsedTicks * 100) / percentof); }