Skip to content

DeepClone, Serialize, DeSerialize extensions useful for testing

License

Notifications You must be signed in to change notification settings

laazyj/GeorgeCloney

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GeorgeCloney

Useful extension methods for deep-cloning an object.

There are 3 extension methods available:

  • Serialize(this object source) returns uses the BinaryFormatter to return a MemoryStream containing the serialized object.
  • Deserialize(this Stream stream) uses BinaryFormatter to recreate an instance of T from the stream.
  • DeepClone(this object source) will return a copy of the object with the same values but not the same instance. The clone is created either using serialization, if the source is serializable, or reflection if it is not. When using reflection all properties are recursed so that it really is a "deep" clone.
	class Test
	{
		public string Value { get; set; }
		public Test Nested { get; set; }
	}
	
	var A = new Test { Value = "something", Nested = new Test { Value = "else" } };
	var B = A.DeepClone();
	
	// A and B are equal but not the same
	Assert.AreEqual(A.Value, B.Value);
	Assert.AreEqual(A.Nested.Value, B.Nested.Value);
	
	// Modifying B does not change A
	B.Nested.Value = "modified";
	Assert.AreNotEqual(A.Nested.Value, B.Nested.Value);

Get GeorgeCloney at NuGet.org: https://www.nuget.org/packages/GeorgeCloney/

About

DeepClone, Serialize, DeSerialize extensions useful for testing

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published