HowTo: Implement IPersistStream in a .NET class

When writing customized objects, many cases need to support serialization. For example, a custom symbol or element needs to be saved with the map document and load when the map document is opened. This functionality is available by implementing the interface IPersistVariant.

To support cloning through serialization, temporarily save the object to an ObjectSteam and then duplicate the object by creating a new instance of the class and loading its properties from the temporary ObjectStream. use an ObjectCopy class, which uses ObjectStream internally. This class requires that the cloned object, or clonee, support IPersistStream.

IPersistStream is a Microsoft interface which provides methods for saving and loading objects that use a simple serial stream for their storage needs. In order to use the structured stream given by these methods using .NET, the objects must be converted into a byte array.

There are several ways to convert objects into byte arrays in .NET, however each method is object type specific. For objects that only have managed class members, use MemoryStream in conjunction with a BinaryFormatter, assuming that all of these members support serialization. The reality is that the object has different types of class members, including both managed and unmanaged types, i.e., ArcObjects components. This makes it a challenge to implement the IPersistStream .Save() and IPersistStream.Load() methods. Writing this code as a part of the custom object can also be cumbersome and difficult to read and maintain.

The solution is to write a helper static class with helper methods Save() and Load () to delegate the calls when implementing IPersistStream in an object.

Further reading…

Sample for  ‘Serialization ArcObjects to memory ‘


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: