使用protobuf-net问题序列化WPF DrawingBrush

时间:2012-04-28 06:31:10

标签: .net wpf serialization protobuf-net

需要序列化包含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可以为null,但在测试一中,它不是。正确序列化的任何变通方法1)基础对象与非null DrawingBrush 2)派生对象与null DrawingBrush?提前谢谢。

2 个答案:

答案 0 :(得分:3)

更正用于序列化简单WPF刷的RuntimeTypeModel配置:

RuntimeTypeModel.Default.Add(typeof(Brush), false)
               .AddSubType(300, typeof(SolidColorBrush));
RuntimeTypeModel.Default.Add(typeof(SolidColorBrush), false)
                .Add("Color");
RuntimeTypeModel.Default.Add(typeof(Color), false)
                .Add("A", "R", "G", "B"); // needed for proper color serialization
RuntimeTypeModel.Default.Add(typeof(SolidColorBrush), false)
        .Add("Color");
RuntimeTypeModel.Default.Add(typeof(DrawingBrush), false)
        .Add("Stretch", "Drawing");
RuntimeTypeModel.Default.Add(typeof(Drawing), false)
        .AddSubType(100, typeof(DrawingGroup))
        .AddSubType(200, typeof(GeometryDrawing));
RuntimeTypeModel.Default.Add(typeof(DrawingGroup), false)
        .Add("Children");
RuntimeTypeModel.Default.Add(typeof(Pen), false)
        .Add("Brush", "Thickness", "LineJoin");
RuntimeTypeModel.Default.Add(typeof(GeometryDrawing), false)
       .Add("Brush", "Geometry", "Pen");

答案 1 :(得分:2)

由于未定义合约,DrawingBrush不会立即进行Serializable。各种选择:

  • 如果类型相当简单,您可以在运行时通过RuntimeTypeModel配置合约,根据需要添加属性/子类型等
  • 在某些情况下,使用“代理”可能是首选 - 具有双向转换运算符的类型,用作动态DTO - 同样,可以通过RuntimeTypeModel指定代理; “代理”让你使用现有的模型,但根据需要交换DTO类型
  • 否则,请考虑避免模型中特定于框架的类型,并将DTO类型与您需要的信息一起使用。从构造运行时框架的期望值