Deep Clone generic Type - CSharp System

CSharp examples for System:Object

Description

Deep Clone generic Type

Demo Code


using System.Runtime.Serialization.Formatters.Binary;
using System.IO;//from   w  ww. j  a  va2s. co m

public class Main{
        public static T Clone<T>(T instance)
        {
            var serializer = new BinaryFormatter();
            var ms = new MemoryStream();

            serializer.Serialize(ms, instance);
            ms.Position = 0;

            return (T)serializer.Deserialize(ms);
        }
}

Related Tutorials