C#深拷贝

最近在做winform项目,遇到页面上有同一个数据源的两个ComboBox,此时操作一个ComboBox,会影响到另一个ComboBox,究其原因,是因为数据源是引用类型,值栈引堆,虽然是不同的ComboBox,但是指针指向同一个内存地址,所以操作一个变量会影响到另一个,为解决这个问题,考虑使用深拷贝的方式。

下边是我适用的深拷贝帮助类

    /// <summary>
    /// 深拷贝
    /// </summary>
    public static class DeepCopyHelper
    {
        /// <summary>
        /// 通过XmlSerializer和内存流
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="RealObject"></param>
        /// <returns></returns>
        public static T Clone<T>(T RealObject)
        {
            using (Stream stream = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(stream, RealObject);
                stream.Seek(0, SeekOrigin.Begin);
                return (T)serializer.Deserialize(stream);
            }
        }
        /// <summary>
        /// 通过JsonConvert,序列化和反序列化操作
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T DeepCopyJson<T>(T obj)
        {// 序列化
            string json = JsonConvert.SerializeObject(obj);
            // 反序列化
            return JsonConvert.DeserializeObject<T>(json);
        }
        /// <summary>
        /// 通过BinaryFormatter和内存流
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static T DeepCopyBinary<T>(T obj)
        {
            object rel; 
            using (MemoryStream ms = new MemoryStream()) 
            { 
                BinaryFormatter bf = new BinaryFormatter(); 
                bf.Serialize(ms, obj); 
                ms.Seek(0, SeekOrigin.Begin); 
                rel = bf.Deserialize(ms); 
                ms.Close(); 
            }
            return (T)rel;
        }
        /// <summary>
        /// 通过DataContractSerializer和内存流
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static T DeepCopyDataContract<T>(T obj)
        {
            object rel; 
            using (MemoryStream ms = new MemoryStream()) 
            { 
                DataContractSerializer ser = new DataContractSerializer(typeof(T)); 
                ser.WriteObject(ms, obj); 
                ms.Seek(0, SeekOrigin.Begin); 
                rel = ser.ReadObject(ms); 
                ms.Close(); 
            }
            return (T)rel;
        }
        /// <summary>
        /// 最简单直接的是,循环变量赋值
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static List<int> DeepCopyData(List<int> list)
        {
            //此处使用new关键字,创建了对象,所以该变量指向了个新的引用
            List<int> listDeep = new List<int>();
            for(var i=0;i<list.Count;i++)
            {
                var item = list[i];
                listDeep.Add(item);
            }
            return listDeep;
        }
    }

 

posted @ 2023-08-23 18:24  阳光下的行者  阅读(44)  评论(0编辑  收藏  举报