【发布时间】:2011-11-10 20:40:20
【问题描述】:
我遇到了涉及以下场景的问题,应用程序 A 有一个 DataGridView,从中选择了对象。它们被拖到应用程序 B 内的 DataGridView 中,并且对象被复制到新列表中。这是App A中的代码,准备拖放:
ArrayList ToDrag = this.GetSelectedBoundItems();
DataObject data = new DataObject(ToDrag);
this.DoDragDrop(data, DragDropEffects.Move | DragDropEffects.Copy);
问题:接收端无法获取ArrayList。这是代码:
//formats has the value of "System.Collections.ArrayList"
var formats = myRetrievedObject.GetFormats();
//candoit has the value "true"
var candoit = myRetrievedObject.GetDataPresent(typeof(ArrayList));
//DraggedItems is of type "System.__ComObject"
var DraggedItems = myRetrievedObject.GetData(typeof(ArrayList));
//this returns null. I 'think' this should work
ArrayList DraggedItems2 = myRetrievedObject.GetData(typeof(ArrayList)) as ArrayList;
//This throws an exception (see below)
ArrayList DraggedItems2 = (ArrayList)myRetrievedObject.GetData(typeof(ArrayList)) as ArrayList;
这曾经工作过。这发生了两件事,我们从 .net 2.0 跳到 .net 4.0,这段代码被翻译成 C++.net 成 C#。
我不确定我错过了什么。该数据应该被转换为数组列表。
感谢您的帮助!
编辑这是演员表中的异常文本
dfResultsControls.dll 中出现“System.InvalidCastException”类型的第一次机会异常
附加信息:无法将“System.__ComObject”类型的 COM 对象转换为“System.Collections.ArrayList”类类型。表示 COM 组件的类型的实例不能转换为不表示 COM 组件的类型;但是,只要底层 COM 组件支持对接口的 IID 的 QueryInterface 调用,它们就可以转换为接口。
【问题讨论】:
-
我做过,但我不完全记得我做了什么。我{很确定}问题是我拖动的项目不可序列化,它们必须用于普通对象拖放。即使您将它们标记为可序列化,对象中的所有类型也必须是可序列化的。
-
@SpectralGhost,如果这不起作用或不适用,请告诉我,我会深入研究代码并尝试记住原因。
标签: c# .net drag-and-drop ipc marshalling