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

C# 序列化派生类时不包括ProtoBuf.net基类属性,c#,.net,class,protobuf-net,C#,.net,Class,Protobuf Net,使用ProtoBuf.net的最新2.0 beta版本,我试图序列化派生类(只是一个示例),结果得到一个空文件。为什么基类属性没有序列化 [ProtoContract] [Serializable] public class Web2PdfClient : Web2PdfEntity { } [ProtoContract] [Serializable] public class Web2PdfEntity : EngineEntity { [ProtoMember(1)]

使用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不查看
[Serializable]
,因此您不需要它,除非您同时使用
二进制格式化程序(或类似工具)


类似地,
enginentity
应该公布其预期的子类型,并且应该指出要序列化的成员(以及哪个标记)。

Marc,我还有一个问题,希望可以使用注释提问,因为它与此问题相关。您说过基类必须用表示继承类的属性来标记。如果基类位于单独的类库中,该怎么办?我无法将[ProtoInclude(7,typeof(MyInheritedClass))]放在基类上,因为我将得到一个错误,即MyInheritedClass未解析。@Tomas-如果基类不知道派生类型,那么您也可以在运行时在v2中使用TypeModel和AddSubType对其进行配置;i、 e.
typeModel.Add(typeof(Web2PdfEntity),false).AddSubType(7,typeof(Web2PdfClient))并使用
typeModel。序列化
etc(缓存并重用模型,因为它涉及生成的IL等)