Copy/paste of generic collection
-
My copy/paste operation results in a MemoryStream full of '\0' characters on the Clipboard. Maybe my approach to copy/paste is wrong when handling generic types? All entries in the CollectionBase are, of course, ISerializable and the binary serialization to file fully works. The collection is, to sketch the context, a set of shapes and connections of a diagram (application), see the Netron project[^] for details. Thank you so much for you help. :rose: The copy operation:
CollectionBase<IDiagramEntity> copy = MyDeepCopyOfTheCollectionViaSerialization();
DataFormats.Format format =
DataFormats.GetFormat(typeof(CollectionBase<IDiagramEntity> ).FullName);
IDataObject dataObject = new DataObject();
dataObject.SetData(format.Name, false, copy);
Clipboard.SetDataObject(dataObject, false);where the MyDeepCopyOfTheCollectionViaSerialization() is :
CollectionBase<T> newobj = null;
MemoryStream stream = new MemoryStream();
GenericFormatter<BinaryFormatter> f = new
GenericFormatter<BinaryFormatter>();
f.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
newobj = f.Deserialize<CollectionBase<T>>(stream);
stream.Close();
return newobj;And finally the Paste operation is:
IDataObject data = Clipboard.GetDataObject();
string format = typeof(CollectionBase<IDiagramEntity> ).FullName;
if (data.GetDataPresent(format))
{
//data.GetData(format) is a MemoryStream rather than a generic collection! X|
}I thought about it,
I stood up
and I did it.
The Netron Project-- modified at 10:59 Saturday 18th March, 2006
-
My copy/paste operation results in a MemoryStream full of '\0' characters on the Clipboard. Maybe my approach to copy/paste is wrong when handling generic types? All entries in the CollectionBase are, of course, ISerializable and the binary serialization to file fully works. The collection is, to sketch the context, a set of shapes and connections of a diagram (application), see the Netron project[^] for details. Thank you so much for you help. :rose: The copy operation:
CollectionBase<IDiagramEntity> copy = MyDeepCopyOfTheCollectionViaSerialization();
DataFormats.Format format =
DataFormats.GetFormat(typeof(CollectionBase<IDiagramEntity> ).FullName);
IDataObject dataObject = new DataObject();
dataObject.SetData(format.Name, false, copy);
Clipboard.SetDataObject(dataObject, false);where the MyDeepCopyOfTheCollectionViaSerialization() is :
CollectionBase<T> newobj = null;
MemoryStream stream = new MemoryStream();
GenericFormatter<BinaryFormatter> f = new
GenericFormatter<BinaryFormatter>();
f.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
newobj = f.Deserialize<CollectionBase<T>>(stream);
stream.Close();
return newobj;And finally the Paste operation is:
IDataObject data = Clipboard.GetDataObject();
string format = typeof(CollectionBase<IDiagramEntity> ).FullName;
if (data.GetDataPresent(format))
{
//data.GetData(format) is a MemoryStream rather than a generic collection! X|
}I thought about it,
I stood up
and I did it.
The Netron Project-- modified at 10:59 Saturday 18th March, 2006
NetronProject wrote:
f.Serialize(stream, this);stream.Seek(0, SeekOrigin.Begin);newobj = f.Deserialize>(stream);
What on earth you doing there? Why not pass the just generated memorystream directly?
-
NetronProject wrote:
f.Serialize(stream, this);stream.Seek(0, SeekOrigin.Begin);newobj = f.Deserialize>(stream);
What on earth you doing there? Why not pass the just generated memorystream directly?
Ah! Interesting, though unorthodox idea. The logic is as follows; I first make a deep copy of the collection such that it's detached from the context. Since it's a copy of the collection is seemed logical to implement a 'Copy()' method. Of course, your suggestion is quite an interesting short-cut and I tried it out but...a Stream instance is not serializable and the pasting results in a 'null' value. On the other hand, an Image object is a kind of stream as well, no? :doh: The Clipboard has never been my favorite. Thanks a bunch for the idea! I have posted the question on Google, MSDN forums, here and you're the only one who has answered. :)
I thought about it,
I stood up
and I did it.
The Netron Project -
Ah! Interesting, though unorthodox idea. The logic is as follows; I first make a deep copy of the collection such that it's detached from the context. Since it's a copy of the collection is seemed logical to implement a 'Copy()' method. Of course, your suggestion is quite an interesting short-cut and I tried it out but...a Stream instance is not serializable and the pasting results in a 'null' value. On the other hand, an Image object is a kind of stream as well, no? :doh: The Clipboard has never been my favorite. Thanks a bunch for the idea! I have posted the question on Google, MSDN forums, here and you're the only one who has answered. :)
I thought about it,
I stood up
and I did it.
The Netron ProjectI have tried something similar, and as long as you remain with an application, rather use a static hashtable and map objects to GUID's or something. In my case I want to keep the proper uncloned references. HTH :)
-
I have tried something similar, and as long as you remain with an application, rather use a static hashtable and map objects to GUID's or something. In my case I want to keep the proper uncloned references. HTH :)
You know what...it's working. It seems things like
stream.Seek(0, SeekOrigin.Begin);
are not a good idea. Thanks! By the way, your XACC project is really impressive. :omg: Kind, F.
I thought about it,
I stood up
and I did it.
The Netron Project