IDataObject 拖/放问题,从 __ComObject 进行转换
我遇到涉及以下场景的问题,应用程序 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 对象强制转换为类类型'系统.集合.ArrayList'。表示 COM 组件的类型实例不能转换为不表示 COM 组件的类型;但是,只要底层 COM 组件支持对接口 IID 的 QueryInterface 调用,它们就可以转换为接口。
I'm having an issue involving the following scenario, Application A has a DataGridView, from which objects are selected. They are dragged into a DataGridView inside Application B, and the objects are copied into the new list. Here is the code in App A, preparing the dragdrop:
ArrayList ToDrag = this.GetSelectedBoundItems();
DataObject data = new DataObject(ToDrag);
this.DoDragDrop(data, DragDropEffects.Move | DragDropEffects.Copy);
The problem: the recieving end is unable to get the ArrayList. Here is the code for that:
//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;
This used to work. Two things have happened to this, we jumped from .net 2.0 to .net 4.0, and this code was translated into C++.net into C#.
I'm not sure what I'm missing. That data is supposed to be translated into an array list.
Thanks for the help!
edit this is the exception text from the cast
A first chance exception of type 'System.InvalidCastException' occurred in dfResultsControls.dll
Additional information: Unable to cast COM object of type 'System.__ComObject' to class type 'System.Collections.ArrayList'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使(数据)对象可序列化解决了我的问题。
Making the (data) object serializable solves the problem for me.