自定义对象类型的跨进程拖放在WinForms C# [英] Cross-Process Drag and Drop of custom object type in WinForms C#

查看:136
本文介绍了自定义对象类型的跨进程拖放在WinForms C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题接近我感兴趣的内容,但不完全。

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

我有一个.NET C#中编写的WinForms应用程序。我有一个 ListView 控件,显示一个C#对象数组。我已经挂接它,以便您可以将这些列表视图项目拖放到同一应用程序中的其他表单中,并正确传递对象数组(键入 Session )到这个其他形式的放置处理程序。

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.

但是,我现在想要支持跨进程拖放,我运行我的应用程序的多个实例。这个出现它将工作(例如 GetDataPresent 成功),但最终会在实际尝试检索数据时抛出异常 - 无法投射 object [] to Session []

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 
   }
}

任何人都知道,如果我必须为我的对象实现 ISerializable ,以使其成为工作?通常,我会尝试一下,但是为这个类实现 ISerializable 将是非常不重要的,我担心可能会有奇怪的副作用所以

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.

更新:实现 ISerializable 没有帮助 - 该方法从未被调用。同样,将$ code> Serializable 属性添加到该类也没有任何影响。任何其他想法?

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?

推荐答案

您正在跨过程边界,对象引用在另一个进程中无效。 DataObject类支持串行化对象以将其跨越墙,它使用BinaryFormatter。所以,是的,您需要将[Serializable]属性应用到您的课堂,并确保您的对象可以正确序列化。

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.

这篇关于自定义对象类型的跨进程拖放在WinForms C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆