Faster deep cloning

前端 未结 9 724
深忆病人
深忆病人 2020-12-02 13:44

Does anyone want a framework/class which allows me to clone by values .Net objects? I\'m only interested with public read/write properties (namely DataContracts), and I don\

9条回答
  •  眼角桃花
    2020-12-02 14:08

    I don't know whether this suits your requirements exactly, but you could also create a deep clone using a BinaryFormatter. See this answer to a related question (by Binoj Antony):

    public static class GenericCopier
    {
        public static T DeepCopy(object objectToCopy)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, objectToCopy);
                memoryStream.Seek(0, SeekOrigin.Begin);
                return (T) binaryFormatter.Deserialize(memoryStream);
            }
        }
    }
    

提交回复
热议问题