Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-Process Drag and Drop of custom object type in WinForms C#

This question is close to what I'm interested in, but not quite.

I have a .NET WinForms application written in C#. I have a ListView control which displays an array of C# objects. I've hooked it up so that you can drag/drop these listview items to a different form in the same application, and it properly passes the array of objects (type Session) to the drop handler for that other form.

However, I now want to support cross-process drag/drop where I run multiple instances of my application. This appears that it's going to work (e.g. GetDataPresent succeeds), but ultimately throws an exception when I actually try to retrieve the data-- cannot cast object[] to Session[].

if (e.Data.GetDataPresent("Fiddler.Session[]"))
{
   Session[] oDroppedSessions;
   try
   {
      oDroppedSessions = (Session[])e.Data.GetData("Fiddler.Session[]");
   }
   catch (Exception eX)
   {  // reaches here 
   }
}

Anyone know if I must implement ISerializable for my objects in order to make this work? Ordinarily, I'd simply try it, but implementing ISerializable for this class would be quite non-trivial, and I'm worried that there may be weird side-effects of doing so.


UPDATE: Implementing ISerializable doesn't help-- the method is never called. Similarly, adding the Serializable attribute to the class has no impact at all. Any other ideas?

like image 395
EricLaw Avatar asked Jan 21 '10 01:01

EricLaw


1 Answers

You are crossing a process boundary, object references are not valid in another process. The DataObject class supports serializing objects to get them across the wall, it uses BinaryFormatter. So, yes, you'll need to apply the [Serializable] attribute to your class and make sure your objects can de/serialize properly.

like image 191
Hans Passant Avatar answered Sep 23 '22 13:09

Hans Passant