自定义对象类型的跨进程拖放包含 WinForms C# 中 XmlNode 的排序列表
我遇到了与用户在以下位置发帖类似的问题:
幸运的是,我已经弄清楚如何序列化自定义对象的几乎所有部分(SortedList 对象除外)。
我需要这个对象,因为它包含一些对我的应用程序非常重要的信息,并且 Xml 嵌套非常混乱。
当我注释掉在 ISerialized 成员 GetObjectData() 中添加 SortedList 的行时,该对象会将其传递到新应用程序。当我把它留在里面时,它没有,而且我不知道如何将其序列化。
我在 StackOverflow 和网络上做了一些查找,但没有发现任何有用的东西。
我正在使用以下代码来检查我的对象是否可序列化,以便可以将其拖动和拖动。转到另一个应用程序:
/// <summary>
/// Determine if object can be fully serializable to binary format.
/// </summary>
/// <param name="obj"></param>
/// <param name="errorMsg">If return value false, contains reason for failure.</param>
/// <returns></returns>
public static bool IsSerializable(object obj, out string errorMsg)
{
errorMsg = "";
using (MemoryStream mem = new MemoryStream())
{
BinaryFormatter bin = new BinaryFormatter();
try
{
bin.Serialize(mem, obj);
return true;
}
catch (Exception ex)
{
errorMsg = string.Format("Object cannot be serialized: {0}", ex.ToString());
return false;
}
}
}
有人有任何建议可以帮助我吗?如果可能的话,我希望在拖/放过程中保持 XmlNode 列表完整,但不反对进行一些额外的编码以将其分解为可序列化的片段并在另一侧重建它。重要的是最终结果必须包含一个SortedList。
如果有必要,我可以提供我正在序列化的自定义对象的内容以进行拖放(如果有帮助的话)。
谢谢,
凯尔·K。
I had a similar problem as the user posting at the following location:
Cross-Process Drag and Drop of custom object type in WinForms C#
Luckily, I have figured out how to serialize almost all parts of my custom object except for a SortedList object.
I need this object because it contains some pretty crucial information to my application, and the Xml nesting is pretty messy.
When I comment out the line adding the SortedList in the ISerializable member GetObjectData(), the object makes it across to the new app. When I leave it in, it doesn't, and I can't figure out how to get it serialized.
I have done some looking both here on StackOverflow and on the web, but have found nothing of use.
I am using the following code to check if my object is serializable so it can be drag & dropped to another application:
/// <summary>
/// Determine if object can be fully serializable to binary format.
/// </summary>
/// <param name="obj"></param>
/// <param name="errorMsg">If return value false, contains reason for failure.</param>
/// <returns></returns>
public static bool IsSerializable(object obj, out string errorMsg)
{
errorMsg = "";
using (MemoryStream mem = new MemoryStream())
{
BinaryFormatter bin = new BinaryFormatter();
try
{
bin.Serialize(mem, obj);
return true;
}
catch (Exception ex)
{
errorMsg = string.Format("Object cannot be serialized: {0}", ex.ToString());
return false;
}
}
}
Does anyone have any suggestions that can help me out? I would like to keep my list of XmlNodes intact during the drag/drop if possible, but wouldn't be opposed to doing some additional coding to break it up into serialize-able pieces and reconstructing it on the other side. The important thing is that the end result must contain a SortedList.
If necessary, I can provide the contents of my custom object I am serializing for drag and drop if it will help.
Thanks,
Kyle K.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于弄清楚如何正确序列化我的对象。我使用的是 XmlNodes 的 SortedList,我发现 XmlNode 对象不可序列化。我将实现切换为包含字符串的 SortedList,现在一切正常。
谢谢,
凯尔
I finally figured out how to correctly serialize my object. I was using a SortedList of XmlNodes, which I found out the XmlNode object is not serializable. I switched my implementation to contain a SortedList of strings, and now everything works just fine.
Thanks,
Kyle