需要序列化一些包含 DrawingBrush 的 BL 结构。我已将其重写如下:
[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
}
要序列化我正在执行以下代码:
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);
}
}
我需要序列化 Derived 类对象,但在 Base 序列化期间,我得到“没有找到合适的默认 DrawingBrush 编码”。认为这是因为 DrawingBrush 对于某些对象可以为空,但在测试中它不是。正确序列化的任何解决方法 1) 具有非空 DrawingBrush 的基础对象 2) 具有空 DrawingBrush 的派生对象?提前致谢。