WinForms C# 中自定义对象类型的跨进程拖放
这个问题与我感兴趣的内容很接近,但不是相当。
我有一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在跨越进程边界,对象引用在另一个进程中无效。 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.
好吧,这是一个镜头,而不是使用整个会话数组,尝试像这样单独进行......
值得一试,除了搬起石头砸自己的脚,因为我不完全理解会话部分,尝试一下......
希望这有帮助,
此致,
汤姆.
Ok this is a shot, instead of using a whole array of Sessions, try doing it individually like this...
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.
您可以使用“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.