Cloning an object using persistence


Purpose
This sample demonstrate an implementation of a clonable object through serialization. Sometimes, when you are writing your objects, you might need to support serialization. For example, a custom symbol or custom element needs to be saved with the map document and reloaded when the map document is opened. In most cases, you can achieve this functionality by implementing the IPersistVariant interface, which gives you an elegant solution. However, in some cases, IPersistVariant is not enough and your object must support IPersisStream, for example, if you need to support cloning through serialization. By temporarily saving your object to an ObjectStream, you can duplicate the object by creating an instance of your class and loading its properties from the temporary ObjectStream. To do so, use an ObjectCopy class (which uses ObjectStream internally).

How to use

See How to use ArcGIS samples for help on compiling, setting up the debugger, and running the sample. If the sample has associated data, you will find that the sample's zip file includes a "data" folder alongside the language folders. However, you will need to update the sample to point to the location of the data once you have extracted all the files.

  1. Start Visual Studio and open the solution.
  2. Build the PeristStreamHelper.csproj, ClonableObj.csproj, and TestApp.csproj projects.
  3. Make sure the TestApp.csproj project is set as the startup project (in the Solution Explorer, right-click the project and select Set as StartUp Project).
  4. Press F5 to run the test application. A console window appears listing the flow of the test application (note the different messages reporting the state of the original and cloned objects).
  5. When done, press any key to close the application.

PeristStreamHelper.cs Helper class that provides helper methods to read and write objects to the structured stream.
ClonableObj/ClonableObjClass.cs An implementation of an object that implements IPersistStream using the helper class.
ClonableObj/TestApp/Program.cs The main entry point for the test application.
ClonableObj/TestApp/TestClass.cs A test class that creates an instance of the clonable object, assigns properties to it, then clones it.
ClonableObj/TestApp/LicenseInitializer.cs ESRI license initialize class.
Download the C# files

Download the files for all languages

See Also:

Implement IPersistStream in a .NET class
Implementing persistence
Implementing cloning
Sample: Triangle graphic element
Sample: Clonable object




Additional Requirements
  • This sample can only be done in C# (and not in VB.NET). This is because "unsafe" isn't supported in VB.NET, and it is needed here because pointers need to be used with this 3rd party library.

Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced