Search code examples
c#winformsdrag-and-drop

Drag and drop custom object


I have 2 controls one one form: list and a tree (specific type names are irrelevant).

In the list control I execute DoDragDrop method. As the first argument I pass an object which was data bound to that row. The object implements a specific interface and is marked with Serializable attribute.

What I want is to retreive that object in DragEnter/DragDrop event handler of the tree control. I'm using the following code:

void TreeControlDragEnter(object sender, DragEventArgs e)
{
    var formats = e.Data.GetFormats();
    var data = e.Data.GetData(typeof (IFoo));
}

Unfortunately, in result data is null and formats is an one-element array which holds the name of the specific type (implementing IFoo). I assume that I would have to pass exact type name to GetData to retreve the object, but it's not possible as it is a private class.

Is there a way to get the object by its interface?


Solution

  • You have to provide the same type as the class that was serialized in the first place. You cannot use an interface or base class of the serialized class because then more than one of the formats might match it and it would not know which one to deserialize. If you have several classes that all implement IFoo and there is an instance of each inside the data object then asking for IFoo would be ambiguous. So you must ask for the exact type that was serialized into the data object.

    This means you should not place classes into the data object that cannot be deserialized because they are private at the other end.