Cross-Process Drag and Drop of custom object type in WinForms C#

假装没事ソ 提交于 2019-12-05 03:09:15

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!