WinForms C# 中自定义对象类型的跨进程拖放

发布于 2024-08-18 18:37:20 字数 1001 浏览 4 评论 0原文

这个问题与我感兴趣的内容很接近,但不是相当。

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

但是,我现在希望支持跨进程拖/放,在其中运行应用程序的多个实例。这似乎它会起作用(例如GetDataPresent成功),但最终在我实际尝试检索数据时抛出异常 - 无法转换object[]Session[]

if (e.Data.GetDataPresent("Fiddler.Session[]"))
{
   Session[] oDroppedSessions;
   try
   {
      oDroppedSessions = (Session[])e.Data.GetData("Fiddler.Session[]");
   }
   catch (Exception eX)
   {  // reaches here 
   }
}

有人知道我是否必须为我的对象实现ISerialized才能完成这项工作吗?通常,我只是简单地尝试一下,但是为此类实现 ISerialized 是非常重要的,而且我担心这样做可能会产生奇怪的副作用。


更新:实现ISerialized没有帮助——该方法永远不会被调用。同样,向类添加 Serialized 属性也没有任何影响。还有其他想法吗?

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

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.

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

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.


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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浊酒尽余欢 2024-08-25 18:37:20

您正在跨越进程边界,对象引用在另一个进程中无效。 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.

另类 2024-08-25 18:37:20

好吧,这是一个镜头,而不是使用整个会话数组,尝试像这样单独进行......

   Session[] oDroppedSessions;
   try
   {
      if (e.Data.GetData("Fiddler.Session[]") != null){
          object[] objs = e.Data.GetData("Fiddler.Session[]");
          if (objs != null && objs.Length > 1){
             oDroppedSessions = new Session[objs.Length];
             int nIndex = 0;
             foreach(object obj in objs){
                if (obj is Session){
                  oDroppedSessions[nIndex] = (Session)obj;
                  nIndex++;
                }
             }
          }
      }
   }
   catch (Exception eX)
   {  // reaches here }

值得一试,除了搬起石头砸自己的脚,因为我不完全理解会话部分,尝试一下......

希望这有帮助,
此致,
汤姆.

Ok this is a shot, instead of using a whole array of Sessions, try doing it individually like this...

   Session[] oDroppedSessions;
   try
   {
      if (e.Data.GetData("Fiddler.Session[]") != null){
          object[] objs = e.Data.GetData("Fiddler.Session[]");
          if (objs != null && objs.Length > 1){
             oDroppedSessions = new Session[objs.Length];
             int nIndex = 0;
             foreach(object obj in objs){
                if (obj is Session){
                  oDroppedSessions[nIndex] = (Session)obj;
                  nIndex++;
                }
             }
          }
      }
   }
   catch (Exception eX)
   {  // reaches here }

Worth a shot, other than shooting myself in the foot as I do not fully understand the Session part, try it...

Hope this helps,
Best regards,
Tom.

乖乖哒 2024-08-25 18:37:20

您可以使用“as”进行转换,这将避免异常(如果转换失败,“as”将返回“null”而不会引发异常) - 但我不认为这会解决您的问题(它只会避免实际的问题)例外),正如我同意的那样,您可能必须使您的类可序列化。您可以通过注释掉更难使其发挥作用的字段来测试您的假设 - 现在只是为了测试它。

You could use "as" for casting which will avoid the exception ("as" will return "null" without throwing an exception if the cast fails) - but I don't think this will solve your problem (it will just avoid the actual exception), as I agree it's likely you'll have to make your class Serializable. You could test your hypothesis by commenting out the fields that will be harder to make it work - just for now to test it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文