C#如何优雅处理引用类型的深拷贝(十一)

几年前写过一个 bug,根因很土:该深拷贝的地方没深拷贝,副本一改,原件跟着变。排查的时候老板以为动的是库里的数据,其实就是一个本地对象被共享了。

先把词说清楚:

浅拷贝:值类型复制一份;引用类型复制的是引用,两边还指着同一个子对象。你改副本里的引用成员,原件也会变。

只复制对象自身的一层:字段/属性里如果是值类型,会复制一份值;如果是引用类型,复制的是引用(指针),新旧对象仍指向同一块堆上的子对象。

深拷贝:引用链上也建新对象,改副本不该动到原件的嵌套数据。

从根对象开始,递归地为引用类型也创建新实例,并把内容复制过去,直到整棵「对象图」在逻辑上独立。改拷贝不应意外改动原对象里的嵌套数据。


ICloneable:能深,但接口不保证

ICloneable 只有一个 object Clone(),文档不会替你承诺浅还是深,看实现。你想做深拷贝,可以,全写在 Clone() 里就行。

浅拷贝场景下,改拷贝里的引用类型字段,往往会影响原对象(反之亦然),除非你再给那个字段赋一个新实例。

https://github.com/abdys-lono/gqwznj/blob/main/cgJaeI5CwQ_2221157.md

https://github.com/jrnov/xwieaw/blob/main/gbvZQAe8c6_1111559.md

https://github.com/kewarh1/dcobxt/blob/main/j0X7H8sMqK_9443377.md

https://github.com/insbrangotekson/hbhkxt/blob/main/9c6aXypZ3X_0001155.md

https://github.com/justinghove/tobnjf/blob/main/mjAXoMTDhB_8509983.md

https://github.com/abdys-lono/gqwznj/blob/main/0xriPqhRvP_4443371.md

https://github.com/jrnov/xwieaw/blob/main/VGGovf9d7b_7777611.md

https://github.com/kewarh1/dcobxt/blob/main/z2AuvSZJnH_1276527.md

https://github.com/insbrangotekson/hbhkxt/blob/main/ghls9gn1Vz_5665499.md

https://github.com/justinghove/tobnjf/blob/main/AUeVCcTDhB_9949872.md

https://github.com/abdys-lono/gqwznj/blob/main/UvpdkUySwP_1266716.md

https://github.com/jrnov/xwieaw/blob/main/heYta1sc6a_9009804.md

https://github.com/kewarh1/dcobxt/blob/main/H7LmfTaKoI_5505430.md

https://github.com/insbrangotekson/hbhkxt/blob/main/kNeEPG0UyS_0110943.md

https://github.com/justinghove/tobnjf/blob/main/8Scx7yiCgA_9049872.md

https://github.com/abdys-lono/gqwznj/blob/main/tkRriSwQuO_6659304.md

https://github.com/jrnov/xwieaw/blob/main/szGnue8c6a_1221055.md

https://github.com/kewarh1/dcobxt/blob/main/spG7qKoImG_5660094.md

https://github.com/insbrangotekson/hbhkxt/blob/main/kr8gnX1zTx_0009887.md

https://github.com/justinghove/tobnjf/blob/main/IF90h8zjDh_2221150.md

https://github.com/abdys-lono/gqwznj/blob/main/Y5gNG4BvPt_6665094.md

https://github.com/jrnov/xwieaw/blob/main/u8YQhEL5Z3_1659354.md

https://github.com/kewarh1/dcobxt/blob/main/KIiZJnHlFj_3443722.md

https://github.com/insbrangotekson/hbhkxt/blob/main/reEvqAKBvP_9444377.md

https://github.com/justinghove/tobnjf/blob/main/Vjg6xhBf9d_1661216.md

https://github.com/abdys-lono/gqwznj/blob/main/dk1ZgQuOsM_6115448.md

https://github.com/jrnov/xwieaw/blob/main/UoRFM6a4Y2_0504266.md

https://github.com/kewarh1/dcobxt/blob/main/D4oImGkEiC_1221055.md

https://github.com/insbrangotekson/hbhkxt/blob/main/eFPG0USwQu_2334832.md

https://github.com/justinghove/tobnjf/blob/main/TAXoLSCgAe_1165943.md

https://github.com/abdys-lono/gqwznj/blob/main/jT04iVcMqK_0110949.md

https://github.com/jrnov/xwieaw/blob/main/4IFgaOVFjC_1609372.md

https://github.com/kewarh1/dcobxt/blob/main/saXxoY2W0U_1666004.md

https://github.com/insbrangotekson/hbhkxt/blob/main/o48FW4BvPt_2221276.md

https://github.com/justinghove/tobnjf/blob/main/FjkkIP9d7b_4494327.md

https://github.com/abdys-lono/gqwznj/blob/main/s9gnX1VzTx_3332266.md

https://github.com/kewarh1/dcobxt/blob/main/vPtMJkbLpJ_2221150.md

https://github.com/jrnov/xwieaw/blob/main/8zCdXKRBf9_5665498.md

https://github.com/insbrangotekson/hbhkxt/blob/main/7lYdKkbLpJ_1221054.md

https://github.com/justinghove/tobnjf/blob/main/fcWq0rb5Z3_3443277.md

https://github.com/kewarh1/dcobxt/blob/main/U5lfTaKoIm_9999377.md

https://github.com/abdys-lono/gqwznj/blob/main/oZ6AnbiSwQ_3443276.md

https://github.com/jrnov/xwieaw/blob/main/kEijjHO8ca_5555385.md

https://github.com/insbrangotekson/hbhkxt/blob/main/Z9rH8sMqKo_6726509.md

https://github.com/justinghove/tobnjf/blob/main/3u75VM6a4Y_2382337.md

https://github.com/kewarh1/dcobxt/blob/main/IC07Ov2mGE_4544386.md

https://github.com/abdys-lono/gqwznj/blob/main/8CJa7EySwQ_5605448.md

https://github.com/jrnov/xwieaw/blob/main/IpwA7YP9d7_1161761.md

https://github.com/insbrangotekson/hbhkxt/blob/main/9qEUWdNrLp_7326504.md

https://github.com/justinghove/tobnjf/blob/main/ElpSGN7b5Z_7887616.md

https://github.com/kewarh1/dcobxt/blob/main/R2jdQXHlFj_4999877.md

https://github.com/abdys-lono/gqwznj/blob/main/eEvpcjTxRv_8990049.md

https://github.com/jrnov/xwieaw/blob/main/QaRBf9d7b5_9999832.md

https://github.com/insbrangotekson/hbhkxt/blob/main/8I9tNrLpJn_6666549.md

https://github.com/justinghove/tobnjf/blob/main/g0B2mGkECg_1665094.md

https://github.com/kewarh1/dcobxt/blob/main/KULZWxoY2W_2222682.md

https://github.com/abdys-lono/gqwznj/blob/main/UOBJZ7EySw_3883226.md

https://github.com/jrnov/xwieaw/blob/main/Ku5vc3ue8c_6776157.md

https://github.com/insbrangotekson/hbhkxt/blob/main/TTU18MqKoI_5054359.md

https://github.com/kewarh1/dcobxt/blob/main/Zk7rsPWGkE_1661054.md

https://github.com/justinghove/tobnjf/blob/main/hBBCjqa4Y2_0160044.md

https://github.com/abdys-lono/gqwznj/blob/main/5Y2WTOFzTx_8443272.md

https://github.com/jrnov/xwieaw/blob/main/ifZQ7YP9d7_3888711.md

https://github.com/insbrangotekson/hbhkxt/blob/main/Opj3gUbLpJ_3833276.md

https://github.com/justinghove/tobnjf/blob/main/CkK1SJ3X1V_7271450.md

https://github.com/kewarh1/dcobxt/blob/main/YiZJnHlFjD_5665596.md

https://github.com/abdys-lono/gqwznj/blob/main/Z445cjTxRv_5665448.md

https://github.com/jrnov/xwieaw/blob/main/nKubVIP9d7_8998771.md

https://github.com/insbrangotekson/hbhkxt/blob/main/JgQQRyZJnH_4546504.md

https://github.com/justinghove/tobnjf/blob/main/1OfCnUvmW0_9093372.md

https://github.com/kewarh1/dcobxt/blob/main/HFf3JryiCg_9948276.md

https://github.com/abdys-lono/gqwznj/blob/main/zQJbiSwQuO_4009838.md

https://github.com/jrnov/xwieaw/blob/main/ZQdb1sc6a4_0110938.md

https://github.com/insbrangotekson/hbhkxt/blob/main/OiM9G0UySw_1223272.md

https://github.com/justinghove/tobnjf/blob/main/V5m9Qx4oIm_1221054.md

https://github.com/kewarh1/dcobxt/blob/main/llmJt3uec6_8386004.md

https://github.com/abdys-lono/gqwznj/blob/main/Fz0YfPtNrL_3332266.md

https://github.com/jrnov/xwieaw/blob/main/cZ0rb5Z3X1_4443372.md

https://github.com/justinghove/tobnjf/blob/main/ZkbolCXHlF_6650993.md

https://github.com/insbrangotekson/hbhkxt/blob/main/w6xhBf9d7b_2221150.md

https://github.com/kewarh1/dcobxt/blob/main/n1USsjTxRv_7282161.md

https://github.com/abdys-lono/gqwznj/blob/main/OpjWdNrLpJ_7889832.md

https://github.com/jrnov/xwieaw/blob/main/Vs9gGQH1Vz_5576654.md

https://github.com/justinghove/tobnjf/blob/main/WgXli90kEh_5788956.md

https://github.com/kewarh1/dcobxt/blob/main/mWX4fMneOs_0000983.md

https://github.com/insbrangotekson/hbhkxt/blob/main/HxLc9G0UyS_5667610.md

https://github.com/abdys-lono/gqwznj/blob/main/7xB8ZQAe86_3938276.md

https://github.com/jrnov/xwieaw/blob/main/xUYBz6qKoI_0555994.md

https://github.com/justinghove/tobnjf/blob/main/7OSZKryiCg_1221372.md

https://github.com/kewarh1/dcobxt/blob/main/DOlVW3AuOs_8993349.md

https://github.com/abdys-lono/gqwznj/blob/main/szjkIP9c6a_3334882.md

https://github.com/jrnov/xwieaw/blob/main/yWcqnE5pJn_1221155.md

https://github.com/insbrangotekson/hbhkxt/blob/main/Ofiq6elVzT_0059105.md

https://github.com/justinghove/tobnjf/blob/main/GhaOVFjDhB_7888766.md

https://github.com/kewarh1/dcobxt/blob/main/ck1YfPtNrL_0554985.md

https://github.com/abdys-lono/gqwznj/blob/main/UIsZTGN7bZ_9071154.md

https://github.com/jrnov/xwieaw/blob/main/hVcMNu1lFj_1221104.md

https://github.com/insbrangotekson/hbhkxt/blob/main/aa7BpcjTxR_3887216.md

https://github.com/justinghove/tobnjf/blob/main/YpMwbSCgAe_1211549.md

https://github.com/kewarh1/dcobxt/blob/main/xuo8JAuOsM_9444382.md

https://github.com/abdys-lono/gqwznj/blob/main/mN4xlsc6a4_9009488.md

https://github.com/jrnov/xwieaw/blob/main/WhYlj90kEi_7711049.md

https://github.com/insbrangotekson/hbhkxt/blob/main/eBI2W0UySw_5005610.md

https://github.com/justinghove/tobnjf/blob/main/jA4ryiCgAe_8887213.md

https://github.com/kewarh1/dcobxt/blob/main/gurH8sMqKo_8332266.md

https://github.com/abdys-lono/gqwznj/blob/main/r2P9AipZX1_5998727.md

https://github.com/jrnov/xwieaw/blob/main/6KIicQXHlF_2398321.md

https://github.com/insbrangotekson/hbhkxt/blob/main/TNhrBMDxRv_6668261.md

https://github.com/justinghove/tobnjf/blob/main/XIJqRBf9d7_2222105.md

https://github.com/kewarh1/dcobxt/blob/main/eoCwxUbLpJ_8998822.md

https://github.com/abdys-lono/gqwznj/blob/main/MXuefCJ3X1_9021150.md

https://github.com/jrnov/xwieaw/blob/main/ARS3kA1lFj_1776043.md

https://github.com/insbrangotekson/hbhkxt/blob/main/WQkvmW0UyS_1111094.md

https://github.com/justinghove/tobnjf/blob/main/v2JqQbSCgA_2332117.md

https://github.com/kewarh1/dcobxt/blob/main/aBsm6H8sMq_2877150.md

https://github.com/abdys-lono/gqwznj/blob/main/lJtb1sc6a4_8871261.md

https://github.com/jrnov/xwieaw/blob/main/erICz6qKoI_1110448.md

https://github.com/insbrangotekson/hbhkxt/blob/main/hHRI2W0UyS_4776509.md

https://github.com/justinghove/tobnjf/blob/main/8warRbSCge_8150993.md

https://github.com/kewarh1/dcobxt/blob/main/p9m6kYfPtN_6768726.md

https://github.com/abdys-lono/gqwznj/blob/main/pQ70ovf9d7_6610483.md

https://github.com/jrnov/xwieaw/blob/main/vPQQyZJnHl_2222172.md

https://github.com/insbrangotekson/hbhkxt/blob/main/1SJX0xOFzT_6798721.md

https://github.com/justinghove/tobnjf/blob/main/LP2qQ8YP9d_2382682.md

https://github.com/kewarh1/dcobxt/blob/main/BPtMKkbLpJ_0009938.md

https://github.com/abdys-lono/gqwznj/blob/main/vS2D4oIlFj_7809832.md

https://github.com/jrnov/xwieaw/blob/main/OYPda0rb5Z_9996559.md

https://github.com/insbrangotekson/hbhkxt/blob/main/quYsVJQA8c_6778721.md

https://github.com/justinghove/tobnjf/blob/main/o1VStkUySw_7448822.md

https://github.com/kewarh1/dcobxt/blob/main/7oi2fTaKoI_1115761.md

https://github.com/jrnov/xwieaw/blob/main/UHPgDK4Y2W_7748211.md

https://github.com/abdys-lono/gqwznj/blob/main/tR1i6t0kEi_1110044.md

https://github.com/justinghove/tobnjf/blob/main/nNXO8c6a4Y_0110549.md

https://github.com/insbrangotekson/hbhkxt/blob/main/vwT4lC3nHl_1161094.md

https://github.com/kewarh1/dcobxt/blob/main/nX48mZgQuO_9099837.md

https://github.com/jrnov/xwieaw/blob/main/f9AAipZ3X1_8998727.md

https://github.com/abdys-lono/gqwznj/blob/main/PS6NR4szjD_4943238.md

https://github.com/kewarh1/dcobxt/blob/main/Y59nahRvPt_6776500.md

https://github.com/justinghove/tobnjf/blob/main/EmM3xkL5Z3_3887216.md

https://github.com/insbrangotekson/hbhkxt/blob/main/ICWD7u1lFj_1778216.md

https://github.com/jrnov/xwieaw/blob/main/PjNhL8FzTx_7221150.md

https://github.com/abdys-lono/gqwznj/blob/main/Pqk3hVcMqK_2332266.md

https://github.com/kewarh1/dcobxt/blob/main/IljDAbSCgA_3450943.md

https://github.com/justinghove/tobnjf/blob/main/FtAErfmW0U_4554383.md

https://github.com/insbrangotekson/hbhkxt/blob/main/OzgZNUEiCg_0000983.md

https://github.com/jrnov/xwieaw/blob/main/zwrhPpguOs_7887115.md

https://github.com/kewarh1/dcobxt/blob/main/C9ZQAe8c6a_5423110.md

https://github.com/abdys-lono/gqwznj/blob/main/gJaeI5CwQu_3943327.md

https://github.com/justinghove/tobnjf/blob/main/sMqKHhYImG_0444822.md

https://github.com/insbrangotekson/hbhkxt/blob/main/HHIqxhBf9d_8887933.md

https://github.com/jrnov/xwieaw/blob/main/Vz00YfPtNr_3887711.md

https://github.com/kewarh1/dcobxt/blob/main/f9d63UL5Z3_8882150.md

https://github.com/justinghove/tobnjf/blob/main/YmD7u1lFDh_2660438.md

https://github.com/abdys-lono/gqwznj/blob/main/fI6gOofPtN_6687716.md

https://github.com/insbrangotekson/hbhkxt/blob/main/CA4vc2td7b_0150993.md

https://github.com/jrnov/xwieaw/blob/main/ySwwRy5pJn_8998727.md

https://github.com/kewarh1/dcobxt/blob/main/QBhlPDK4Y2_0555433.md

https://github.com/justinghove/tobnjf/blob/main/xbsvZNUEiC_0022150.md

https://github.com/abdys-lono/gqwznj/blob/main/o8m6jXeOsM_0110165.md

https://github.com/insbrangotekson/hbhkxt/blob/main/GklmJQAe8c_5004329.md

https://github.com/jrnov/xwieaw/blob/main/wxxU5F6qKo_1110549.md

https://github.com/kewarh1/dcobxt/blob/main/XhYImGkEig_8998727.md

https://github.com/justinghove/tobnjf/blob/main/URsFW4BvOs_2322105.md

https://github.com/abdys-lono/gqwznj/blob/main/Iwjrbc9G0U_3337276.md

https://github.com/insbrangotekson/hbhkxt/blob/main/4KszjDhBf9_7249872.md

https://github.com/jrnov/xwieaw/blob/main/F9x4LszjDh_0559950.md

https://github.com/abdys-lono/gqwznj/blob/main/pcGX7I9tNL_4544382.md

https://github.com/kewarh1/dcobxt/blob/main/f9d64UL5Z3_8326004.md

https://github.com/justinghove/tobnjf/blob/main/bcjTxRvPtN_2332155.md

https://github.com/insbrangotekson/hbhkxt/blob/main/B5P3qxhBf9_1283276.md

https://github.com/abdys-lono/gqwznj/blob/main/ySST18sMpJ_2776050.md

https://github.com/kewarh1/dcobxt/blob/main/yHvjqa4Y2W_3882283.md

https://github.com/jrnov/xwieaw/blob/main/0B1FCdUEiC_5555493.md

https://github.com/justinghove/tobnjf/blob/main/HYcF3AuOsM_7887660.md

https://github.com/insbrangotekson/hbhkxt/blob/main/ANoiVcMqKI_5610543.md

https://github.com/abdys-lono/gqwznj/blob/main/YsWqUHO8c6_4446655.md

https://github.com/kewarh1/dcobxt/blob/main/De1lmJQAe8_5665054.md

https://github.com/jrnov/xwieaw/blob/main/gkOiM9G0Uy_7227660.md

https://github.com/justinghove/tobnjf/blob/main/QNHbmdNrLp_0009933.md

https://github.com/abdys-lono/gqwznj/blob/main/6xhBf9d7b5_9009882.md

https://github.com/insbrangotekson/hbhkxt/blob/main/ofPtNrLpJn_9054389.md

https://github.com/jrnov/xwieaw/blob/main/hbPWnKRBf9_3882160.md

https://github.com/kewarh1/dcobxt/blob/main/p2TNAH1VzT_1110044.md

https://github.com/justinghove/tobnjf/blob/main/uofMmdNrLp_0100988.md

https://github.com/abdys-lono/gqwznj/blob/main/6xB8ZP9d7b_0132110.md

https://github.com/insbrangotekson/hbhkxt/blob/main/ZNUhe5wgAe_2244327.md

https://github.com/kewarh1/dcobxt/blob/main/T03hVcMqKo_9490482.md

https://github.com/jrnov/xwieaw/blob/main/cddBI2W0Uy_5100943.md

https://github.com/justinghove/tobnjf/blob/main/ixxU4F6qKo_3337716.md

https://github.com/abdys-lono/gqwznj/blob/main/BczGr1sc6a_6610943.md

https://github.com/kewarh1/dcobxt/blob/main/EYCz6qKoIm_0009983.md

https://github.com/jrnov/xwieaw/blob/main/JArI9tNrLp_6666504.md

https://github.com/insbrangotekson/hbhkxt/blob/main/Nbc9G0UySw_9009382.md

https://github.com/justinghove/tobnjf/blob/main/TeVFjDhBf9_5667611.md

https://github.com/abdys-lono/gqwznj/blob/main/4opMTDhBf9_7887660.md

https://github.com/kewarh1/dcobxt/blob/main/oypZ3X1VzT_4993438.md

https://github.com/insbrangotekson/hbhkxt/blob/main/DezDgd4vf9_7777610.md

https://github.com/justinghove/tobnjf/blob/main/RvPsqG7rLp_8838838.md

https://github.com/jrnov/xwieaw/blob/main/NH4BvtNrLp_1609372.md

相关推荐
范什么特西10 小时前
idea里面jsp找不到图片
java·开发语言·servlet
23.10 小时前
【分析】网关故障排查指南:如何区分并定位‘上游’与‘下游’问题
java
风风筝吖10 小时前
ACL + NAT 综合配置HCL
运维·网络
不是山谷.:.10 小时前
websocket的封装
开发语言·前端·网络·笔记·websocket·网络协议
liguojun202510 小时前
软硬一体智慧场馆系统推荐——助力场馆数字化高效升级
java·大数据·人工智能·物联网·1024程序员节
晨曦中的暮雨10 小时前
4.17小厂HR面
java
青云计划10 小时前
渐进式发布
java·后端
小则又沐风a10 小时前
深入理解进程概念 第三章 进程调度切换
java·linux·服务器·前端
努力攀登的小k10 小时前
《Java基础,Java多态入门到进阶:重写、重载、转型的逻辑与实战避坑》
java·开发语言