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?
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
ISerializableand add a IgnoreDataMemberAttribute to yourColorProperty. Also add some serialization-only property like this: