如何在C#中创建没有任何引用的类对象的副本(克隆对象) [英] How to create copy of class object without any reference in C# (Cloning an Object)
问题描述
我有一个包含私有变量,公共属性和私有/公共成员函数等的类.
I have a class with private variables, public properties and private/public member functions, etc.
我已经创建了此类的实例,并对该对象进行了一些操作.
I have created an instance of this class and did some manipulations on this object.
出于某种原因,我想创建该实例的副本/克隆而无需引用原始对象.
I want to create the Copy/Clone of this instance without having reference to the original object for some reason.
但是复制/克隆的对象应与原始对象完全相同.所有私有变量,公共变量,属性的值都应与原始对象中的值相同.
But the copied/cloned object should be exactly same as original object. All private variables, public variables, properties should have the same value as in Original Object.
请帮助我该怎么做?
请让我了解复制/克隆对象的最佳方法是什么?
Please let me what is the best way to Copy/Clone the object?
在这个问题上,我需要一些专家的帮助.
I need some experts help on this issue.
推荐答案
你好,
实现此目的最简单的方法是使用ICloneable,
The easiest and simplest mode to achieve this is to use ICloneable,
有关ICloneable的实现,请查看此 文章中,您会找到两种解释的克隆方法:shallow&
For an ICloneable implementation please take a look at this article , where you will find both cloning methods explained: shallow & deep.
希望这会有所帮助,如果您还有其他问题或意见,请告诉我,
最好的问候,
伊曼纽尔·瓦尔加(Emanuel Varga)
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
这篇关于如何在C#中创建没有任何引用的类对象的副本(克隆对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!