RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: .NET
Expertise: Advanced
Dec 21, 2005

Retrieving Custom Objects Added to the Clipboard

You may have found that sometimes, copying a custom .NET object to the clipboard doesn't work. That's because retrieving a custom object from the clipboard returns nothing if the object has a reference to a nonserializable object. The clipboard is using serialization to return a copy of the object. The object is added to the clipboard without any errors. The object shows present, but this comes back as nothing.

Most times, the offending item in the object is obvious. However, because the serialization also includes public events, any other object that is listening for an event raised by the object is included in the serialization. If any of those objects is not serializable then it fails!

The easiest way around this is to implement the Iserializable interface for your object. The serializable interface lets you decide which items of your object are serialized. You can then avoid any that might refer to other nonserializable objects—like events. Remember that an object implementing the Iserializable interface must create a special constructor. Deserializing fails if this constructor is not implemented. The interface does not automatically generate the constructor.

Robert Pfeffer
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date