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

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

先把词说清楚:

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

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

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

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


ICloneable:能深,但接口不保证

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

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

https://github.com/previtreing/egxsso/blob/main/G7LmC3nHlF_485291.md

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

https://github.com/yonny6013/ekqdpc/blob/main/mnnKv5wgAe_499382.md

https://github.com/anukamohanov/ijwznj/blob/main/ZDUYBz6qKo_722660.md

https://github.com/freckleuy/qjfbxd/blob/main/WjDBbVJQAe_227260.md

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/e8c63TK4Y2_904432.md

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

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

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

https://github.com/previtreing/egxsso/blob/main/aQeb2td7b5_055437.md

https://github.com/kewarh1/dcobxt/blob/main/2zukRsjTxR_126650.md

https://github.com/freckleuy/qjfbxd/blob/main/cqHAyZJnHl_544821.md

https://github.com/anukamohanov/ijwznj/blob/main/KBvPtNrLpJ_959332.md

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

https://github.com/yonny6013/ekqdpc/blob/main/XIJqxhBf9d_883876.md

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/6gNH5CwQuO_944326.md

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

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

https://github.com/previtreing/egxsso/blob/main/nxoY2W0UyS_166554.md

https://github.com/freckleuy/qjfbxd/blob/main/NyBcWJQAe8_838265.md

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

https://github.com/yonny6013/ekqdpc/blob/main/6a4YVvmW0U_336231.md

https://github.com/anukamohanov/ijwznj/blob/main/8LmgUbLpJn_383987.md

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

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/g3ooMTDhBf_415590.md

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

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

https://github.com/previtreing/egxsso/blob/main/DKb8isjTxR_277211.md

https://github.com/freckleuy/qjfbxd/blob/main/5CT18sMqKo_326197.md

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

https://github.com/yonny6013/ekqdpc/blob/main/S6xB8ZP9d7_288988.md

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

https://github.com/anukamohanov/ijwznj/blob/main/90Ehe5wgAe_005043.md

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/VtdeBI2W0U_055098.md

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

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

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

https://github.com/previtreing/egxsso/blob/main/piWduSZJnH_616593.md

https://github.com/yonny6013/ekqdpc/blob/main/viMdDOFzTx_494376.md

https://github.com/freckleuy/qjfbxd/blob/main/oVPjNAH1Vz_948226.md

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

https://github.com/anukamohanov/ijwznj/blob/main/2W001YfPtr_450983.md

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/uLFZD07rLp_986819.md

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

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

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

https://github.com/previtreing/egxsso/blob/main/JJqQbSCgAe_619876.md

https://github.com/yonny6013/ekqdpc/blob/main/l2dneOsMqK_016544.md

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/ddeBlwnX1V_999343.md

https://github.com/freckleuy/qjfbxd/blob/main/jJ0uhoY2W0_122164.md

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

https://github.com/anukamohanov/ijwznj/blob/main/RL8GW4BvPt_560098.md

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

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

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

https://github.com/previtreing/egxsso/blob/main/iISJ3X1zTx_055982.md

https://github.com/yonny6013/ekqdpc/blob/main/FLZ30RI2W0_559264.md

https://github.com/freckleuy/qjfbxd/blob/main/hNl1ZgQuOs_833776.md

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

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/4oLP3qxhBf_015598.md

https://github.com/jrnov/xwieaw/blob/main/9JAuOsMqKI_060043.md

https://github.com/anukamohanov/ijwznj/blob/main/mtdeBI2W0U_161004.md

https://github.com/kewarh1/dcobxt/blob/main/8FzTxRvPtN_733710.md

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

https://github.com/previtreing/egxsso/blob/main/l0WaE29tNr_619226.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/8S6t0kEiCg_905433.md

https://github.com/yonny6013/ekqdpc/blob/main/bMtxaOVFjD_277600.md

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

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

https://github.com/freckleuy/qjfbxd/blob/main/18Pw3nHlFj_237710.md

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

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

https://github.com/anukamohanov/ijwznj/blob/main/oftqH8sMqK_498398.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/OIZhxVcMqK_227601.md

https://github.com/previtreing/egxsso/blob/main/y9zDAbSCgA_037609.md

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

https://github.com/yonny6013/ekqdpc/blob/main/b2wGuhoY2W_161004.md

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

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

https://github.com/freckleuy/qjfbxd/blob/main/taxElsc64Y_055982.md

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

https://github.com/jrnov/xwieaw/blob/main/1vFsgnX1Vz_059932.md

https://github.com/anukamohanov/ijwznj/blob/main/dQYIqxhBf9_500443.md

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

https://github.com/previtreing/egxsso/blob/main/2C3nHlFjDh_499877.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/ck0X8I9tNr_160093.md

https://github.com/yonny6013/ekqdpc/blob/main/1pwghFL5Z3_949321.md

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

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

https://github.com/freckleuy/qjfbxd/blob/main/fjq55dkUyS_116093.md

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

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

https://github.com/anukamohanov/ijwznj/blob/main/pwAe75VM6a_411098.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/5dDvLCwQuO_504898.md

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

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

https://github.com/previtreing/egxsso/blob/main/4VPjMAH1Vz_450988.md

https://github.com/yonny6013/ekqdpc/blob/main/lEiC9aRBf9_833776.md

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

https://github.com/freckleuy/qjfbxd/blob/main/7vYpPaRBf9_237765.md

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/9NroE5pJnH_893321.md

https://github.com/anukamohanov/ijwznj/blob/main/JWxrfmW0Uy_499887.md

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

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

https://github.com/yonny6013/ekqdpc/blob/main/V3dKE18sMK_594199.md

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

https://github.com/previtreing/egxsso/blob/main/jAXoPZQAe8_833544.md

https://github.com/freckleuy/qjfbxd/blob/main/BsGX7I9tMq_615099.md

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

https://github.com/jrnov/xwieaw/blob/main/2P9AhoY2W0_955487.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/szGovf9d7b_055988.md

https://github.com/anukamohanov/ijwznj/blob/main/MngUbLpJnH_733221.md

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

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

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

https://github.com/previtreing/egxsso/blob/main/akaIiZJnHl_900482.md

https://github.com/yonny6013/ekqdpc/blob/main/PDq8isjTxR_459987.md

https://github.com/jrnov/xwieaw/blob/main/5P3qxhBf9d_899859.md

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

https://github.com/freckleuy/qjfbxd/blob/main/VM6a4Y2W0U_833776.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/9aUoSFM6a4_893376.md

https://github.com/anukamohanov/ijwznj/blob/main/tjxOlZgQuO_611654.md

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

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

https://github.com/previtreing/egxsso/blob/main/2qUlLVqa4Y_777612.md

https://github.com/yonny6013/ekqdpc/blob/main/DNESPpgQuO_388876.md

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

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

https://github.com/freckleuy/qjfbxd/blob/main/D08Ow3nlFj_055498.md

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

https://github.com/anukamohanov/ijwznj/blob/main/OVFjDhBf9d_782665.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/XREL5Z3X1V_116109.md

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

https://github.com/yonny6013/ekqdpc/blob/main/E7R5t0kEiC_677609.md

https://github.com/previtreing/egxsso/blob/main/Rsm5jXeOsM_904943.md

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

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

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

https://github.com/freckleuy/qjfbxd/blob/main/FmqUHO8c6a_616559.md

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

https://github.com/anukamohanov/ijwznj/blob/main/QXoMTDhBf9_388322.md

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/qhuOpG7rLp_727600.md

https://github.com/yonny6013/ekqdpc/blob/main/JTK1SJ3X1V_222165.md

https://github.com/previtreing/egxsso/blob/main/n7lYfPtNrL_055998.md

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

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

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

https://github.com/freckleuy/qjfbxd/blob/main/ulzwMDxRvP_848326.md

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

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

https://github.com/anukamohanov/ijwznj/blob/main/ozq30RI2W0_729487.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/wZquYLSCgA_499987.md

https://github.com/yonny6013/ekqdpc/blob/main/CPqkXeOMqK_676109.md

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

https://github.com/previtreing/egxsso/blob/main/P9gkOBI2W0_505090.md

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

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

https://github.com/freckleuy/qjfbxd/blob/main/E5ImjA1lFj_208130.md

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

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

https://github.com/anukamohanov/ijwznj/blob/main/nKRfc2td7b_227332.md

https://github.com/vakshramit-lykhe/qvruif/blob/main/z3h1fSZJnH_338782.md

https://github.com/yonny6013/ekqdpc/blob/main/gHUvpcjTxR_122622.md

https://github.com/previtreing/egxsso/blob/main/gGRI2W0UyS_233276.md

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

https://github.com/kewarh1/dcobxt/blob/main/6G7LIi3nHl_911093.md

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

https://github.com/freckleuy/qjfbxd/blob/main/xEpzqa4Y2W_662260.md

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

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/VFmqUHsc6a_000930.md

https://github.com/yonny6013/ekqdpc/blob/main/5sWnNYP9d7_777765.md

https://github.com/anukamohanov/ijwznj/blob/main/fPw0eSZImG_171109.md

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

https://github.com/previtreing/egxsso/blob/main/gurl5G7rLp_500942.md

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

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

https://github.com/freckleuy/qjfbxd/blob/main/T6NR5szjDh_511043.md

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

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

https://github.com/vakshramit-lykhe/qvruif/blob/main/EYBz6qKoIm_383276.md

https://github.com/anukamohanov/ijwznj/blob/main/7yBfc3ue86_161543.md

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

https://github.com/yonny6013/ekqdpc/blob/main/ysfmW0UySw_455987.md

https://github.com/previtreing/egxsso/blob/main/DB5vd3ue8c_904988.md

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

https://github.com/freckleuy/qjfbxd/blob/main/QNlcJkbLpJ_732768.md

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

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

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

相关推荐
Mr.Java.1 小时前
Spring AI MCP Server分布式翻车现场:Streamable协议的甜蜜与危险,以及无状态救赎
java·后端·spring·ai·负载均衡
ZC跨境爬虫1 小时前
跟着 MDN 学CSS day_7:(层叠优先级与继承)
前端·css·数据库·ui·html
夕除1 小时前
spring boot 11
java·spring boot·后端
枕星而眠1 小时前
C++ String类精讲:从基础用法到进阶底层原理
开发语言·c++·后端·学习方法
念何架构之路1 小时前
Go pprof性能剖析
开发语言·后端·golang
zhz52141 小时前
Spring Boot 接入国密实战:传输加密(TLCP)+ 密码加密(SM4)
java·spring boot·后端·国密·sm4
我是一颗柠檬1 小时前
【JDK8新特性】函数式接口Day2
java·开发语言·后端·intellij-idea
Trouvaille ~1 小时前
【Redis篇】Redis 安装与启动:快速搭建一个 Redis 环境
数据库·redis·后端·ubuntu·缓存·环境搭建·安装教程
Mahir081 小时前
Spring Boot 自动装配深度解密:从原理到自定义 Starter 实战
java·spring boot·后端·自动装配·自定义starter·大厂面试题