Object - DeepClone

A T extension method that makes a deep copy of '@this' object.

Try it

public static void Main()
{
	var Obj = new TestClass {Value = "Fizz"};

	// C# Extension Method: Object - DeepClone
    TestClass clone = Obj.DeepClone();
	Console.WriteLine("{0} = {1} : {2}", Obj.Value, clone.Value, Obj.Value == clone.Value);
	
}

[Serializable]
public class TestClass
{
    public string Value;
}

View Source
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public static partial class Extensions
{
    /// <summary>
    ///     A T extension method that makes a deep copy of '@this' object.
    /// </summary>
    /// <typeparam name="T">Generic type parameter.</typeparam>
    /// <param name="this">The @this to act on.</param>
    /// <returns>the copied object.</returns>
    public static T DeepClone<T>(this T @this)
    {
        IFormatter formatter = new BinaryFormatter();
        using (var stream = new MemoryStream())
        {
            formatter.Serialize(stream, @this);
            stream.Seek(0, SeekOrigin.Begin);
            return (T) formatter.Deserialize(stream);
        }
    }
}