ProtoBuf.net序列化派生类时不包括基类属性

问题描述

|| 使用ProtoBuf.net的最新2.0 beta版本,我尝试序列化派生类(仅作为示例),并且得到空文件。为什么基类属性未序列化?
[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{

}

[ProtoContract]
[Serializable]
public class Web2PdfEntity : EngineEntity
{

    [ProtoMember(1)]
    public string Title { get; set; }
    [ProtoMember(2)]
    public string CUrl { get; set; }
    [ProtoMember(3)]
    public string FileName { get; set; }

}


[ProtoContract]
[Serializable]
public class EngineEntity
{

    public bool Result { get; set; }
    public string ErrorMessage { get; set; }
    public bool IsMembershipActive { get; set; }
    public int ConversionTimeout { get; set; }
    public byte[] FileStorage { get; set; }
}
使用下面的代码序列化类时,我得到了空文件。
var Web2PDF = new Web2PdfClient
                          {                                
                              CUrl = \"http://www.google.com\",FileName = \"test.txt\"
                          };
        using (var file = File.Create(@\"C:\\Users\\Administrator\\Projects\\temp\\test.bin\"))
        {
            Serializer.Serialize(file,Web2PDF);

        }
    

解决方法

实际上,我很惊讶没有抛出异常-我将进行调查!为了使它起作用,基本类型必须具有唯一的方式来指示每个子类型。可以通过属性指定,也可以在运行时(在v2中)指定。例如:
[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{

}

[ProtoContract]
[ProtoInclude(7,typeof(Web2PdfClient))]
[Serializable]
public class Web2PdfEntity : EngineEntity
{ ... }
7
没什么特别之处,除了它不应与为此类型定义的任何其他成员发生冲突。可以定义多个子类型(具有不同的标签)。还要注意,protobuf-net不会显示ѭ4look,因此除非您也使用
BinaryFormatter
(或类似名称),否则您不需要。 同样,“ 6”应宣告其预期的子类型,并应指出要序列化的成员(以及针对哪个标签)。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...