C#如何优雅处理引用类型的深拷贝 (九)
几年前写过一个 bug,根因很土:该深拷贝的地方没深拷贝,副本一改,原件跟着变。排查的时候老板以为动的是库里的数据,其实就是一个本地对象被共享了。
先把词说清楚:
浅拷贝:值类型复制一份;引用类型复制的是引用,两边还指着同一个子对象。你改副本里的引用成员,原件也会变。
只复制对象自身的一层:字段/属性里如果是值类型,会复制一份值;如果是引用类型,复制的是引用(指针),新旧对象仍指向同一块堆上的子对象。
深拷贝:引用链上也建新对象,改副本不该动到原件的嵌套数据。
从根对象开始,递归地为引用类型也创建新实例,并把内容复制过去,直到整棵「对象图」在逻辑上独立。改拷贝不应意外改动原对象里的嵌套数据。
ICloneable:能深,但接口不保证
ICloneable 只有一个 object Clone(),文档不会替你承诺浅还是深,看实现。你想做深拷贝,可以,全写在 Clone() 里就行。
浅拷贝场景下,改拷贝里的引用类型字段,往往会影响原对象(反之亦然),除非你再给那个字段赋一个新实例。
https://github.com/kewarh1/dcobxt/blob/main/ZkbLpJmGkE_5665094.md
https://github.com/jrnov/xwieaw/blob/main/ayms63ULZ3_2221155.md
https://github.com/insbrangotekson/hbhkxt/blob/main/jU15iWdNrL_9009938.md
https://github.com/justinghove/tobnjf/blob/main/lFjge4vf9d_4889504.md
https://github.com/kewarh1/dcobxt/blob/main/IcmdNrLpJn_1116044.md
https://github.com/abdys-lono/gqwznj/blob/main/FtgHyOFzTx_4554983.md
https://github.com/jrnov/xwieaw/blob/main/HvFtDrelVz_7887765.md
https://github.com/insbrangotekson/hbhkxt/blob/main/TX8Px4oImG_7899837.md
https://github.com/jrnov/xwieaw/blob/main/v9aTHO8c6a_4554837.md
https://github.com/justinghove/tobnjf/blob/main/b8iPm3biSw_6664438.md
https://github.com/kewarh1/dcobxt/blob/main/9QU8S5t0kE_6609837.md
https://github.com/abdys-lono/gqwznj/blob/main/EySwPNneOs_6776509.md
https://github.com/insbrangotekson/hbhkxt/blob/main/29QxXiZJnH_3332266.md
https://github.com/justinghove/tobnjf/blob/main/EL5cgK7EyS_6776509.md
https://github.com/jrnov/xwieaw/blob/main/dneOsMqKoI_2382165.md
https://github.com/kewarh1/dcobxt/blob/main/ZquYsWJQAe_0056054.md
https://github.com/abdys-lono/gqwznj/blob/main/Yps0Kymsc6_0004988.md
https://github.com/kewarh1/dcobxt/blob/main/0HLzJwkrb5_3443872.md
https://github.com/jrnov/xwieaw/blob/main/C3Gh4LszjD_6660049.md
https://github.com/justinghove/tobnjf/blob/main/OYP9d7b5Z3_8888226.md
https://github.com/insbrangotekson/hbhkxt/blob/main/ak5pJnHlFj_2221105.md
https://github.com/abdys-lono/gqwznj/blob/main/KbfJdH4BvP_9009933.md
https://github.com/insbrangotekson/hbhkxt/blob/main/X48l3dneOs_3384482.md
https://github.com/justinghove/tobnjf/blob/main/YztCqelVzT_8998822.md
https://github.com/insbrangotekson/hbhkxt/blob/main/8zjDhBf9d7_5665460.md
https://github.com/abdys-lono/gqwznj/blob/main/RBgghEL5Z3_4599332.md
https://github.com/jrnov/xwieaw/blob/main/1evzAUeVFj_9443276.md
https://github.com/justinghove/tobnjf/blob/main/DKY59nahRv_1092531.md
https://github.com/insbrangotekson/hbhkxt/blob/main/R82M3xkrb5_1221594.md
https://github.com/kewarh1/dcobxt/blob/main/cF3AuvTaKo_5559932.md
https://github.com/abdys-lono/gqwznj/blob/main/iITJXUvmW0_0000948.md
https://github.com/jrnov/xwieaw/blob/main/PWjh81pQAe_0122105.md
https://github.com/justinghove/tobnjf/blob/main/Z9NoiVcMqK_4998876.md
https://github.com/insbrangotekson/hbhkxt/blob/main/AhIVwqdkUy_4504438.md
https://github.com/kewarh1/dcobxt/blob/main/VC6Q4ryiCg_9910509.md
https://github.com/abdys-lono/gqwznj/blob/main/wjJ0uioY2W_1111599.md
https://github.com/jrnov/xwieaw/blob/main/vLCPqkXeOs_8887327.md
https://github.com/justinghove/tobnjf/blob/main/NH4izZkbLp_9009933.md
https://github.com/insbrangotekson/hbhkxt/blob/main/MQXoMTDhBf_0004337.md
https://github.com/kewarh1/dcobxt/blob/main/5zJxHuipZ3_5504483.md
https://github.com/jrnov/xwieaw/blob/main/XBzctUeVFj_3443226.md
https://github.com/abdys-lono/gqwznj/blob/main/1yPJdH4BvP_2221105.md
https://github.com/insbrangotekson/hbhkxt/blob/main/ubVnuBipZ3_0559832.md
https://github.com/justinghove/tobnjf/blob/main/E5JGhbOVFj_8998872.md
https://github.com/kewarh1/dcobxt/blob/main/MdAlStjTxR_7771054.md
https://github.com/jrnov/xwieaw/blob/main/v2nKN1pwgA_0943721.md
https://github.com/abdys-lono/gqwznj/blob/main/2cndroF6qK_6666549.md
https://github.com/insbrangotekson/hbhkxt/blob/main/QDr8CpdkUy_6150115.md
https://github.com/justinghove/tobnjf/blob/main/dUEiCgAe8c_7226600.md
https://github.com/kewarh1/dcobxt/blob/main/0xuIdneOsM_5615591.md
https://github.com/jrnov/xwieaw/blob/main/WKRhFpzqa4_7998721.md
https://github.com/abdys-lono/gqwznj/blob/main/xXhYmj90kE_5665094.md
https://github.com/insbrangotekson/hbhkxt/blob/main/NhsjTxRuOs_5555435.md
https://github.com/justinghove/tobnjf/blob/main/A71LWrb5Z3_5000938.md
https://github.com/kewarh1/dcobxt/blob/main/8Mnh1eSZJn_8809938.md
https://github.com/jrnov/xwieaw/blob/main/VPjMAH1VzT_2221105.md
https://github.com/abdys-lono/gqwznj/blob/main/CdXKRBf9d7_7887716.md
https://github.com/insbrangotekson/hbhkxt/blob/main/zMdAH1VzTx_6666509.md
https://github.com/justinghove/tobnjf/blob/main/BwwxUbLpJn_3322382.md
https://github.com/kewarh1/dcobxt/blob/main/dR5Mw6xhBf_1665499.md
https://github.com/jrnov/xwieaw/blob/main/jGqXuBjqa4_2221155.md
https://github.com/abdys-lono/gqwznj/blob/main/CMDuLgQuOs_3822600.md
https://github.com/justinghove/tobnjf/blob/main/TaLswZNUEi_2276605.md
https://github.com/insbrangotekson/hbhkxt/blob/main/OIcGaD18sM_3888226.md
https://github.com/kewarh1/dcobxt/blob/main/CghiFM64Y2_5665591.md
https://github.com/abdys-lono/gqwznj/blob/main/5F6JHhYImG_1159559.md
https://github.com/jrnov/xwieaw/blob/main/r9jtkUySwQ_9011555.md
https://github.com/justinghove/tobnjf/blob/main/Lw9aUHO8c6_9998844.md
https://github.com/insbrangotekson/hbhkxt/blob/main/vPPQy5pJmG_2211554.md
https://github.com/kewarh1/dcobxt/blob/main/41vFPjulVz_5776605.md
https://github.com/abdys-lono/gqwznj/blob/main/qqNyf6xhBf_7777615.md
https://github.com/jrnov/xwieaw/blob/main/cCtKBvtNrL_7726099.md
https://github.com/justinghove/tobnjf/blob/main/LznQhISJ3X_5655493.md
https://github.com/insbrangotekson/hbhkxt/blob/main/5mC3nHlFjD_4444327.md
https://github.com/kewarh1/dcobxt/blob/main/FM67BpcjTx_9998822.md
https://github.com/abdys-lono/gqwznj/blob/main/Tg71ovf9d7_5009983.md
https://github.com/jrnov/xwieaw/blob/main/3ARz6qKoIm_6665660.md
https://github.com/justinghove/tobnjf/blob/main/tho5cCNEyS_5665498.md
https://github.com/insbrangotekson/hbhkxt/blob/main/90kECgAe8c_1221504.md
https://github.com/kewarh1/dcobxt/blob/main/MhriSwQuOs_2332760.md
https://github.com/abdys-lono/gqwznj/blob/main/3RBCjqa4Y2_8337637.md
https://github.com/jrnov/xwieaw/blob/main/eSZpNx7yiC_3443276.md
https://github.com/justinghove/tobnjf/blob/main/yvp9qkXeOs_9099832.md
https://github.com/insbrangotekson/hbhkxt/blob/main/LyIwGuhoY2_3983278.md
https://github.com/kewarh1/dcobxt/blob/main/8CK4cjTxRv_6776509.md
https://github.com/abdys-lono/gqwznj/blob/main/29uRU8w3nH_9009888.md
https://github.com/jrnov/xwieaw/blob/main/Dgd4vf9d7b_4504438.md
https://github.com/justinghove/tobnjf/blob/main/TXh2jcQXHl_5664943.md
https://github.com/insbrangotekson/hbhkxt/blob/main/DNhsiQqhRv_4443321.md
https://github.com/abdys-lono/gqwznj/blob/main/m6G7oE5pJn_8887713.md
https://github.com/kewarh1/dcobxt/blob/main/li93N1IP9d_1221372.md
https://github.com/jrnov/xwieaw/blob/main/gGxKb8FzTx_9211054.md
https://github.com/justinghove/tobnjf/blob/main/vZNUEFnue8_2332261.md
https://github.com/insbrangotekson/hbhkxt/blob/main/YcF3AuOsMK_3355993.md
https://github.com/kewarh1/dcobxt/blob/main/whEIvjqa4Y_1660944.md
https://github.com/abdys-lono/gqwznj/blob/main/HicwZNUEiC_7758192.md
https://github.com/jrnov/xwieaw/blob/main/KcFW7H8sMq_6616561.md
https://github.com/justinghove/tobnjf/blob/main/DkLVL3TK4Y_8998716.md
https://github.com/insbrangotekson/hbhkxt/blob/main/EcPzgaNUEi_1110094.md
https://github.com/kewarh1/dcobxt/blob/main/9kxOI5CwQu_8910599.md
https://github.com/abdys-lono/gqwznj/blob/main/RBec63UL5Z_2226327.md
https://github.com/jrnov/xwieaw/blob/main/leSZqOVFjD_9443377.md
https://github.com/justinghove/tobnjf/blob/main/lVW37kYfPt_7859937.md
https://github.com/insbrangotekson/hbhkxt/blob/main/YOc3wkrb5Z_9460548.md
https://github.com/kewarh1/dcobxt/blob/main/nh1fSZnHlF_9993226.md
https://github.com/abdys-lono/gqwznj/blob/main/DrfIZAKBvP_7887660.md
https://github.com/jrnov/xwieaw/blob/main/Re5zmtd7b5_6998322.md
https://github.com/justinghove/tobnjf/blob/main/LIC3kA1lFj_5002150.md
https://github.com/insbrangotekson/hbhkxt/blob/main/cdAkvmW0US_7776605.md
https://github.com/kewarh1/dcobxt/blob/main/WnKvbVJQAe_0110548.md
https://github.com/abdys-lono/gqwznj/blob/main/03BRz6qKoI_7721655.md
https://github.com/jrnov/xwieaw/blob/main/ADLb9G0UyS_1661094.md
https://github.com/justinghove/tobnjf/blob/main/0kllIt3ue8_9099872.md
https://github.com/insbrangotekson/hbhkxt/blob/main/25DxyVcMqK_9998875.md
https://github.com/kewarh1/dcobxt/blob/main/FMaXysfmW0_7221488.md
https://github.com/abdys-lono/gqwznj/blob/main/j9Uh82pwgA_5554432.md
https://github.com/jrnov/xwieaw/blob/main/dHaEYC07qK_2276559.md
https://github.com/justinghove/tobnjf/blob/main/q41vFQH1Vz_4009387.md
https://github.com/insbrangotekson/hbhkxt/blob/main/h1C3nHlFjD_2721150.md
https://github.com/kewarh1/dcobxt/blob/main/rfIZ9KfPtN_6666226.md
https://github.com/abdys-lono/gqwznj/blob/main/RBf9ca0rb5_5665548.md
https://github.com/jrnov/xwieaw/blob/main/IjdxbOVFjD_5600948.md
https://github.com/justinghove/tobnjf/blob/main/Pm37IcmdNr_3998327.md
https://github.com/insbrangotekson/hbhkxt/blob/main/vZNUEFmtd7_0150943.md
https://github.com/kewarh1/dcobxt/blob/main/sJ9NroF6qK_0110094.md
https://github.com/abdys-lono/gqwznj/blob/main/Cn1RL9G0Uy_4443327.md
https://github.com/jrnov/xwieaw/blob/main/qu5v96XO8c_5555493.md
https://github.com/justinghove/tobnjf/blob/main/neOsMqKoIm_5056505.md
https://github.com/insbrangotekson/hbhkxt/blob/main/BBiJTK4Y2W_2222682.md
https://github.com/kewarh1/dcobxt/blob/main/4HF9zh7yiC_2221155.md
https://github.com/abdys-lono/gqwznj/blob/main/Hr1s63TK4Y_1221000.md
https://github.com/jrnov/xwieaw/blob/main/6AHY5CwQus_2276004.md
https://github.com/justinghove/tobnjf/blob/main/R1CZJKszjD_1148748.md
https://github.com/insbrangotekson/hbhkxt/blob/main/yM6AHY6DxR_6665093.md
https://github.com/kewarh1/dcobxt/blob/main/1mJNXr2td7_8333216.md
https://github.com/jrnov/xwieaw/blob/main/ZWxrBpcjTx_6110044.md
https://github.com/abdys-lono/gqwznj/blob/main/rI9NqnE5pJ_1111096.md
https://github.com/justinghove/tobnjf/blob/main/2JMUijGN7b_0000948.md
https://github.com/insbrangotekson/hbhkxt/blob/main/aBOpjWdNrL_7799832.md
https://github.com/kewarh1/dcobxt/blob/main/4UL5Z3X1Vz_2776505.md
https://github.com/abdys-lono/gqwznj/blob/main/nlC5P3ryiC_5555498.md
https://github.com/jrnov/xwieaw/blob/main/hvLF3AuOMq_9982266.md
https://github.com/justinghove/tobnjf/blob/main/FLZWxoY2W0_9998822.md
https://github.com/insbrangotekson/hbhkxt/blob/main/nDaLLt0kEi_5380810.md
https://github.com/kewarh1/dcobxt/blob/main/HX5fMG3AuO_3832260.md
https://github.com/abdys-lono/gqwznj/blob/main/0eS5Nx7yiC_9004332.md
https://github.com/jrnov/xwieaw/blob/main/ayls53TK4Y_4998827.md
https://github.com/justinghove/tobnjf/blob/main/lVVW3dneOs_6711054.md
https://github.com/insbrangotekson/hbhkxt/blob/main/f6xhf9d7b5_8887711.md
https://github.com/kewarh1/dcobxt/blob/main/sJDWAy5pJn_2222100.md
https://github.com/jrnov/xwieaw/blob/main/cc9kRsjTxR_0000943.md
https://github.com/abdys-lono/gqwznj/blob/main/v6wd4vf9d7_2221059.md
https://github.com/justinghove/tobnjf/blob/main/l5iW6oEZJn_5665495.md
https://github.com/insbrangotekson/hbhkxt/blob/main/9eeeBmwnX1_5665594.md
https://github.com/kewarh1/dcobxt/blob/main/rOyf2JqxhB_5509937.md
https://github.com/jrnov/xwieaw/blob/main/sI9MnhUbLp_2771105.md
https://github.com/abdys-lono/gqwznj/blob/main/M5Z3XUvmW0_1221504.md
https://github.com/justinghove/tobnjf/blob/main/g7UEEFnue8_6771155.md
https://github.com/insbrangotekson/hbhkxt/blob/main/izW7ohVcMq_1159337.md
https://github.com/kewarh1/dcobxt/blob/main/Cmxo1yPG0U_4444549.md
https://github.com/jrnov/xwieaw/blob/main/90Ehf5wgAe_7221604.md
https://github.com/abdys-lono/gqwznj/blob/main/9qkYfwTaKo_8938771.md
https://github.com/justinghove/tobnjf/blob/main/gkNfFPG0Uy_7327031.md
https://github.com/insbrangotekson/hbhkxt/blob/main/D4IF90kEiC_7227660.md
https://github.com/kewarh1/dcobxt/blob/main/kQK8FW4BuO_3498198.md
https://github.com/jrnov/xwieaw/blob/main/Hr1s63UL5Z_0132110.md
https://github.com/abdys-lono/gqwznj/blob/main/h1CZKKszjD_2221155.md
https://github.com/justinghove/tobnjf/blob/main/a7iPI6DRvP_8998721.md
https://github.com/insbrangotekson/hbhkxt/blob/main/FjDhijGN7b_5665761.md
https://github.com/kewarh1/dcobxt/blob/main/VZgxUbLpJn_5776054.md
https://github.com/jrnov/xwieaw/blob/main/lMdAH1VzTx_3448728.md
https://github.com/abdys-lono/gqwznj/blob/main/7vZqQaRBf9_4554387.md
https://github.com/justinghove/tobnjf/blob/main/qhvsJAuOsM_3488827.md
https://github.com/insbrangotekson/hbhkxt/blob/main/nNbYSmxoY2_3465498.md
https://github.com/kewarh1/dcobxt/blob/main/9wXE7v2mkE_1159337.md
https://github.com/jrnov/xwieaw/blob/main/Jaeo8JAuOs_3333266.md
https://github.com/abdys-lono/gqwznj/blob/main/LTkHO8c6a4_6766549.md
https://github.com/justinghove/tobnjf/blob/main/ZjaoHFfWGk_1115961.md
https://github.com/insbrangotekson/hbhkxt/blob/main/xo2zPG0UyS_8888766.md
https://github.com/kewarh1/dcobxt/blob/main/quYLwd3ue8_3832105.md
https://github.com/jrnov/xwieaw/blob/main/ZAOLFZkbLp_5527655.md
https://github.com/abdys-lono/gqwznj/blob/main/FgXHlFjDhB_0004488.md
https://github.com/justinghove/tobnjf/blob/main/ZTnQELZ3X1_1110504.md
https://github.com/insbrangotekson/hbhkxt/blob/main/1236EU29tN_1183271.md
https://github.com/kewarh1/dcobxt/blob/main/1MWtefCJ3X_4504387.md
https://github.com/jrnov/xwieaw/blob/main/KHBVf0A1lF_2332165.md
https://github.com/justinghove/tobnjf/blob/main/dhL8jQqhRv_0110943.md
https://github.com/abdys-lono/gqwznj/blob/main/P3ryijGN7b_0000166.md
https://github.com/insbrangotekson/hbhkxt/blob/main/2cmdLIjaKo_9099337.md
https://github.com/kewarh1/dcobxt/blob/main/WxKb8itkUy_8998278.md
https://github.com/jrnov/xwieaw/blob/main/VjA3ryiCgA_7223272.md
https://github.com/justinghove/tobnjf/blob/main/ANoi2fTaKo_5605494.md
https://github.com/abdys-lono/gqwznj/blob/main/fCnTr89G0U_1266048.md
https://github.com/insbrangotekson/hbhkxt/blob/main/5Nx7yiCgAe_5005498.md
更多推荐
所有评论(0)