How to make a copy of a reference type [duplicate]

纵然是瞬间 提交于 2019-11-28 07:37:29

问题


Possible Duplicate:
Cloning objects in C#

I have a class with properties and some of them are reference types (instances of other classes) themselves (CookieContainer).

I want to have a exact copy of this class so any change to previous version would not affect this new instance.

Is there a general solution for this kind of problems or I should do it manually ?


回答1:


You need to deep copy the object to another object. There are many approaches to this but serializing one object and deserializing that data into another object is a very quick trick to achieve this. See it here: How do you do a deep copy of an object in .NET (C# specifically)?




回答2:


Decide how deep you want your copy and implement the ICloneable interface and Clone method: http://msdn.microsoft.com/en-us/library/system.icloneable.aspx



来源:https://stackoverflow.com/questions/5843958/how-to-make-a-copy-of-a-reference-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!