custom serialization

Mar 25, 2011 at 2:53 PM

I would like to be able to save my db objects asynchronously, but I can't because one of my properties is a SolidColorBrush. It would probably work, however, if I used a custom serializer. What would a custom serializer for a SolidColorBrush look like? Some example code would be greatly appreciated. Thanks.

Coordinator
Mar 27, 2011 at 5:23 PM

It would look something like this - details to hook it up are here:

http://www.sterlingdatabase.com/sterling-user-guide/5-serializers

public class SolidBrushSerializer : BaseSerializer  
{
    public override bool CanSerialize(Type targetType)
    {
        return targetType.Equals(typeof (SolidColorBrush));                
    }

    public override void Serialize(object target, BinaryWriter writer)
    {
        var data = (SolidColorBrush)target;
        writer.Write(data.A);
        writer.Write(data.R);
        writer.Write(data.G);
        writer.Write(data.B);
    }

    public override object Deserialize(Type type, BinaryReader reader)
    {
        return new SolidColorBrush(Color.FromArgb(
            reader.ReadByte(), reader.ReadByte(),
            reader.ReadByte(), reader.ReadByte());
    }
}