gpt4 book ai didi

c# - IDataObject 拖放问题,从 __ComObject 转换

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:07 24 4
gpt4 key购买 nike

我有一个涉及以下场景的问题,应用程序 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#。

我不确定我错过了什么。该数据应该被转换成数组列表。

感谢您的帮助!

编辑这是类型转换中的异常(exception)文本

dfResultsControls.dll 中发生类型为“System.InvalidCastException”的第一次机会异常

附加信息:无法将“System.__ComObject”类型的 COM 对象转换为类类型“System.Collections.ArrayList”。表示 COM 组件的类型的实例不能转换为不表示 COM 组件的类型;但是,只要底层 COM 组件支持对接口(interface) IID 的 QueryInterface 调用,它们就可以转换为接口(interface)。

最佳答案

使(数据)对象可序列化为我解决了这个问题。

关于c# - IDataObject 拖放问题,从 __ComObject 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8085995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com