如何使一个引用类型的副本 [英] How to make a copy of a reference type
问题描述
可能重复:结果
的 C>克隆对象
我有属性和一些类他们是引用类型(其他类的实例)本身(的CookieContainer
)。
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 ?
推荐答案
您需要深拷贝对象到另一个对象。有许多方法,但这种序列化一个对象和反序列化的数据到另一个对象是一个非常快速的把戏来实现。看到它在这里:怎么办你做一个深拷贝的对象在.net中(C#专)?
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 an object in .Net (C# specifically)?
这篇关于如何使一个引用类型的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!