4
使用ProtoBuf.net的最新2.0 beta版我試圖序列化派生類(只是例子),我得到空文件。爲什麼基類屬性沒有序列化?ProtoBuf.net序列化派生類時不包括基類屬性
[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);
}
Marc,我還有其他問題,希望可以使用評論,因爲它與這個問題有關。你說過,基類必須標有指明繼承類的屬性。如果基類位於分離的類庫中該怎麼辦?我不能在基類上放置[ProtoInclude(7,typeof(MyInheritedClass))],因爲我會得到MyInheritedClass未解析的錯誤。 – Tomas 2011-06-22 06:28:08
@Tomas - 如果基類型不知道派生類型,那麼您也可以在運行時使用TypeModel和AddSubType在v2中對其進行配置;即typeModel.Add(typeof(Web2PdfEntity),false).AddSubType(7,typeof(Web2PdfClient));'並使用'typeModel.Serialize'等等(緩存和重用模型,因爲它涉及到生成的IL等) – 2011-06-22 06:40:28