Clone an Object Using Serialization

Category:
C#, VB.NET, ASP.NET
Type:
Snippets
Difficulty:
Intermediate
Author:
Intelligent Solutions Inc.

Version Compatibility: Visual Basic.NET, ASP.NET

More information:
This snippet allows you to create a copy (clone) of an object by serializing the original into a memory stream and then deserilaizing it out. It also contains a demo which illustrates how to use the code and the difference between cloning an object using this method as opposed to creating a new reference to it using the = operator.

Note that the object copied must be marked as serializiable. If the class definition for the object contains a member that is not marked as serializable, the method will fail (refer to the comments in declarations for more info).

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: