1
我有序列化的基类。protobuf-net将基类反序列化为继承类
[ProtoContract]
public class Web2PdfEntity
{
[ProtoMember(1)]
public string Title { get; set; }
[ProtoMember(2)]
public string CUrl { get; set; }
}
我想反序列化Web2PdfEntity类是从Web2PdfEntity继承Web2PdfServer。
public class Web2PdfServer : Web2PdfEntity
{
public void MyServerMethod {}
public void MyServerMethod2{}
}
我试图使用下面的代码来反序列化类,不幸的是属性没有设置。
var web2Pdf = Serializer.Deserialize<Web2PdfServer>("c:\Web2PdfEntity-class-to-serialize-file.bin");
web2Pdf.Title //<- not deserialized
web2Pdf.CURL //<- not deserialized
我无法在Web2PdfEntity类中添加[ProtoContract,ProtoInclude(8,typeof(Web2PdfServer))]]属性,因为Web2PdfEntity将在类库(分离的程序集)中编译,因为安全原因将不包含Web2PdfServer类。是否有可能以其他方式解决问题? – Tomas
@Marc,我试图使用你的代码,并尝试反序列化类时发生错误System.InvalidCastException'发生在protobuf-net.dll中var web2Pdf = Serializer.Deserialize(“c:\ Web2PdfEntity-class-to-序列化-file.bin“); –
Tomas
@Tomas - 所写的代码应该没问题;你的意思是“我用你的代码减去属性”?不能使用attribs,请参阅更新 –