none
Custom Control Serialization issue RRS feed

  • Question


  • I have a custom control which is inheriting "System.Windows.Controls.ContentControl". When i am trying to serialize my control it throws below exception.

    my control:

    [Serializable()]
        public class VeeObjectControl : ContentControl

    {
    }

    I am using BinaryFormatter for serialization.

    Type 'System.Windows.Controls.ContentControl' in Assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.

    Thursday, November 24, 2016 3:06 AM

Answers

  • If you want an object to be serializable (WPF or otherwise) using the BinaryFormatter, then it needs to know how. Applying the [Serializable] attribute is sufficient only if the base class also has the attributes and any fields of a type that the formatter cannot serialize are marked with the [NotSerialized] attribute.

    If you want an object to be serializable but inherit from an object not already serializable, then you might be able to manage it by implementing ISerializable. Though, without access to the internals of the base class, you might not be able to make the serialized data complete.

    [Serializable]
    class VeeObjectControl : ContentControl, ISerializable
    {
        public VeeObjectControl()
        {
        }
    
        VeeObjectControl(SerializationInfo info, StreamingContext context)
        {
            Value = info.GetString("Value");
        }
    
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Value", Value);
        }
    
        public string Value { get; set; }
    }
    

    Having said all that, the BinaryFormatter seems like a fairly odd choice for serializing a WPF control.

    • Marked as answer by Amit_210988 Friday, March 3, 2017 1:21 AM
    Thursday, November 24, 2016 10:55 AM
  • Hi Amit,

    Based on your code, I've also made a test on my side. The result is same as yours.

    So as Sheng Jiang said, you cannot really serialize a WPF control.  Because your custom class inherits ContentControl. .ContentControl' in Assembly 'PresentationFramework is not marked as serializable.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Tuesday, November 29, 2016 7:27 AM
    • Marked as answer by Amit_210988 Friday, March 3, 2017 1:21 AM
    Friday, November 25, 2016 6:56 AM

All replies

  • well you can't really serialize a WPF control. If you added data to your custom class, extra the data to another class and serialize the data class instead.


    Visual C++ MVP

    • Proposed as answer by Kristin Xie Tuesday, November 29, 2016 7:26 AM
    Thursday, November 24, 2016 5:58 AM
  • If you want an object to be serializable (WPF or otherwise) using the BinaryFormatter, then it needs to know how. Applying the [Serializable] attribute is sufficient only if the base class also has the attributes and any fields of a type that the formatter cannot serialize are marked with the [NotSerialized] attribute.

    If you want an object to be serializable but inherit from an object not already serializable, then you might be able to manage it by implementing ISerializable. Though, without access to the internals of the base class, you might not be able to make the serialized data complete.

    [Serializable]
    class VeeObjectControl : ContentControl, ISerializable
    {
        public VeeObjectControl()
        {
        }
    
        VeeObjectControl(SerializationInfo info, StreamingContext context)
        {
            Value = info.GetString("Value");
        }
    
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Value", Value);
        }
    
        public string Value { get; set; }
    }
    

    Having said all that, the BinaryFormatter seems like a fairly odd choice for serializing a WPF control.

    • Marked as answer by Amit_210988 Friday, March 3, 2017 1:21 AM
    Thursday, November 24, 2016 10:55 AM
  • Hi Amit,

    Based on your code, I've also made a test on my side. The result is same as yours.

    So as Sheng Jiang said, you cannot really serialize a WPF control.  Because your custom class inherits ContentControl. .ContentControl' in Assembly 'PresentationFramework is not marked as serializable.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Tuesday, November 29, 2016 7:27 AM
    • Marked as answer by Amit_210988 Friday, March 3, 2017 1:21 AM
    Friday, November 25, 2016 6:56 AM