June 2003
Intermediate to advanced
800 pages
34h 20m
English
You want to create an exact copy of a serializable object. This is useful if the object doesn’t expose a dedicate Clone method.
Serialize the object to a memory stream, and then deserialize the memory stream.
Serialization provides a quick and convenient way to clone an object, provided it’s serializable. You can use any formatter to perform this magic, but BinaryFormatter makes the most sense (because it requires the least amount of memory). You use the MemoryStream class as an intermediary.
The following example clones the serializable Person object from recipe Create a Serializable Object. In order to use it as written, you’ll need to import the System.Runtime.Serialization.Formatters.Binary ...
Read now
Unlock full access