如何使用Protobuf.net C#序列化自定义对象基本属性

时间:2014-03-10 11:16:35

标签: c# serialization deserialization protobuf-net

我有一个继承PictureBox控件的自定义类,当我反序列化对象时,我缺少所有基本属性,例如“Name”。这是班级。

    [ProtoContract]
[ProtoInclude(100,typeof(PictureBox))]
class Card : PictureBox
{        
    [ProtoMember(1)]
    public string CardId { get; set; }

    [ProtoMember(2)]
    public string CardName { get; set; }

    [ProtoMember(3)]
    public string CardColor { get; set; }

    [ProtoMember(4)]
    public string CardType { get; set; }

    [ProtoMember(5)]
    public string CardRarity { get; set; }

    [ProtoMember(6)]
    public bool Tapped { get; set; }

    [ProtoMember(7)]
    public bool Revealed { get; set; }
}

2 个答案:

答案 0 :(得分:1)

如果你不能修改基类,一种可能性(未经测试)是揭示你需要的东西:

[ProtoMember(8)]
public new string Name
{
    get { return base.Name; }
    set { base.Name = value; }
}

我正在使用这种技术将自己的属性(或更改DefaultAttribute值)应用于标准控件的某些属性,在创建自己的控件时(例如,我自己的Label)似乎工作。

答案 1 :(得分:0)

我的理解是你可能还需要基类的[ProtoContract]属性。 http://www.codeproject.com/Articles/642677/Protobuf-net-the-unofficial-manual