且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建一个对象在C#中的副本

更新时间:2023-11-23 09:43:28

有没有内置的方法。你可以有MyClass的贯彻落实 IClonable 接口(但它是那种德precated),或者只写你自己的复制/ Clone方法。在这两种情况下,你必须写一些code。

There is no built-in way. You can have MyClass implement the IClonable interface (but it is sort of deprecated) or just write your own Copy/Clone method. In either case you will have to write some code.

有关大对象,你可以考虑序列化+反序列化(通过一个MemoryStream),只是为了重新使用现有的code。

For big objects you could consider Serialization + Deserialization (through a MemoryStream), just to reuse existing code.

不管是什么方法,仔细思考什么是复制的意思完全相同。它应该有多深,是否有标识字段的除外等。

Whatever the method, think carefully about what "a copy" means exactly. How deep should it go, are there Id fields to be excepted etc.