相关疑难解决方法(0)

如何使用protobuf-net序列化不可变的封闭类型?

例如,我想序列化和反序列化System.Drawing.Font这是不可变的,不能进行更改以适应protobuf-net约定。通常,是否可以在protobuf-net中编写某种“自定义”序列化程序?

编辑:根据公认的答案,以下是代理的示例System.Drawing

[ProtoContract]
struct ProtoColor
{
    [ProtoMember(1, DataFormat=DataFormat.FixedSize)]
    public uint argb;
    public static implicit operator Color(ProtoColor c) 
        { return Color.FromArgb((int)c.argb); }
    public static implicit operator ProtoColor(Color c)
        { return new ProtoColor { argb = (uint)c.ToArgb() }; }
}
[ProtoContract()]
class ProtoFont
{
    [ProtoMember(1)]
    string FontFamily;
    [ProtoMember(2)]
    float SizeInPoints;
    [ProtoMember(3)]
    FontStyle Style;

    public static implicit operator Font(ProtoFont f) {
        return new Font(f.FontFamily, f.SizeInPoints, f.Style);
    }
    public static implicit operator ProtoFont(Font f) { 
        return f == null …
Run Code Online (Sandbox Code Playgroud)

protobuf-net

4
推荐指数
1
解决办法
1170
查看次数

标签 统计

protobuf-net ×1