Using ISerializable breaks serialization for previously supported types

110 Views Asked by At

I'm trying to serialize/deserialize a custom class I have that has a System.Windows.Media.Color property. It wasn't working because Color doesn't have the [Serializable] Attribute. Based on some other articles, I decided to implement ISerializable, which would let me define custom serialization logic for the Color type.

Color now gets serialized properly, but now my other properties (bytes, floats, strings) are not being serialized. Do I really need to define explicit serialization logic for every individual property when I implement ISerializable even if they were already supported in the first place?

        #region Serialization
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            string colorString = new ColorConverter().ConvertToString(_RgbColor);
            info.AddValue("System.Windows.Media.Color", colorString, typeof(string));
        }
        public AbstractColorModifier(SerializationInfo info, StreamingContext context)
        {
            string colorString = (string)info.GetValue("System.Windows.Media.Color", typeof(string));
            _RgbColor = (Color)ColorConverter.ConvertFromString(colorString);
        }

        #endregion Serialization

        #region Properties
        private Color _RgbColor = Colors.Black;
        public Color RgbColor {
            get { return _RgbColor; }
            set
            {
                if (_RgbColor != value) {
                    _RgbColor = value;
                    NotifyPropertyChanged("RgbColor");
                }
            }
        }

        private byte _ByteColor = 0;
        public byte ByteColor {
            get { return _ByteColor; }
            set { ... }
        }
        private float _FloatColor = 0;
        public float FloatColor {
            get { return _FloatColor; }
            set { ... }
        }
        #endregion Properties

I would have expected the byte float and other properties that were working before to keep working. Is there some way I can avoid explicitly handling their serialization?

2

There are 2 best solutions below

4
X39 On BEST ANSWER

As already mentioned, you need to serialize everything if you ask explicitly for it using ISerializable.

To get that fixed, you got 2 very simple solutions now:

Solution 1 Provide the serialization for all types you want to have serialized

Solution 2 Remove ISerializable and add a IgnoreDataMemberAttribute to your Color Property. Also add some serialization-only property like this:

public string ColorSerialized
{
    get => new ColorConverter().ConvertToString(_RgbColor);
    set => _RgbColor = (Color)ColorConverter.ConvertFromString(value);
}
3
mjwills On

As per the docs:

When GetObjectData is called during serialization, you are responsible for populating the SerializationInfo provided with the method call.

In other words, you are responsible for serialising (and de-serialising) all of the data you are interested (not just Color).