There is a need to serialize some BL structure that contains DrawingBrush. I’ve rewritten it as follows :
[ProtoContract]
public class BaseProtoBuf : INotifyPropertyChanged, IFormattable
{
[ProtoMember(1)]
public string ID { get; set; }
// Bunch of properties of .net primitive types
// ..
private DrawingBrush _geometry;
[ProtoMember(9)]
[Browsable(false)]
public DrawingBrush Geometry
{
get { return _geometry; }
set
{
_geometry = value;
ScaleDrawing();
}
}
}
[ProtoContract]
[ProtoInclude(1, typeof(string))]
// All other includes
[ProtoInclude(9, typeof(DrawingBrush)]
public class DerivedProtoBuf : BaseProtoBuf, ICloneable
{
// Some additional properties of primitive types, annotated starting with ProtoMember 10 and so on
}
To serialize i’m executing following code :
const string fileName = "Protobuf.bin";
using (var file = File.Create(fileName))
{
file.Position = 0;
var testBase = new BaseProtoBuf
{
Height = 100,
Width = 100,
Name = "Test 1",
OffsetX = 200,
OffsetY = 200,
Geometry = sourceList[0].Geometry // some not-null DrawingBrush
};
Serializer.Serialize(file, testBase);
file.Position = 0;
var restored = Serializer.Deserialize<BaseProtoBuf>>(file);
}
}
I need to serialize Derived class object, but during Base serialization i get “No suitable Default DrawingBrush encoding found”. Thought it’s because DrawingBrush can be null for some objects, but in test one it isn’t. Any workarounds to properly serialize 1) Base object with not-null DrawingBrush 2) Derived object with null DrawingBrush ? Thanks in advance.
Correct RuntimeTypeModel configuration for serializing of simple WPF Brush :