I have a class which I marked as [Serializable] that I’m trying to copy through the clipboard. Calling GetData() always returns null.
Copy code:
IDataObject dataObject = new DataObject();
dataObject.SetData("MyClass", false, myObject);
Clipboard.SetDataObject(dataObject, true);
Paste code:
if (Clipboard.ContainsData("MyClass"))
{
IDataObject dataObject = Clipboard.GetDataObject();
if (dataObject.GetDataPresent("MyClass"))
{
MyClass myObject = (MyClass)dataObject.GetData("MyClass");
// myObject is null
}
}
MyClass is actually a derived class. Both it and its base are flagged as [Serializable]. I tried the same code with a simple test class and it worked.
MyClass contains GraphicsPath, Pen, Brush, and arrays of value types.
The Pen class is not marked as serializable, and also inherits from MarshalByRefObject.
You will need to implement ISerializable and handle these types of objects