Thursday, January 3, 2013

How can i object clone


//Required using System.Runtime.Serialization;
        //- Silverlight doesn't have it in its plugin, have to add reference to your project

        public static T DeepCopy<T>(this T theSource)
        {
            T theCopy;
            DataContractSerializer theDataContactSerializer = new DataContractSerializer(typeof(T));
            using (MemoryStream memStream = new MemoryStream())
            {
                theDataContactSerializer.WriteObject(memStream, theSource);
                memStream.Position = 0;
                theCopy = (T)theDataContactSerializer.ReadObject(memStream);
            }
            return theCopy;
        }

No comments:

Post a Comment