开发者

Cross-Process Drag and Drop of custom object type contain sorted list of XmlNodes in WinForms C#

开发者 https://www.devze.com 2023-03-08 04:50 出处:网络
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#

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开发者_如何学C 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号