C# 实体类的深拷贝

来源:互联网 发布:来电通替代软件 编辑:程序博客网 时间:2024/05/23 18:35
    [Serializable]
    public class Data : ICloneable
    {


        public int Level { get; set; }
        public string ID { get; set; }
        public string EID { get; set; }
        public string Name { get; set; }
        public List<Data> Childs { get; set; }
      
        public object Clone()
        {
            using (MemoryStream ms = new MemoryStream(1000))
            {
                object CloneObject;
                BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
                bf.Serialize(ms, this);
                ms.Seek(0, SeekOrigin.Begin);
                // 反序列化至另一个对象(即创建了一个原对象的深表副本) 
                CloneObject = bf.Deserialize(ms);
                // 关闭流 
                ms.Close();
                return CloneObject;
            }




        }
    }
0 0
原创粉丝点击