我有一類設置這樣的(我刪除了很多其他的屬性,因爲它們不涉及到我的問題...):的Protobuf(德)序列化
public class Record : IRecord
{
/// <summary>
/// The type of recovery record and data needed to recover this record.
/// </summary>
[DataMember(Order = 1)]
public IRecord Entry { get; private set; }
[IgnoreDataMember]
public object Entry
{
get { return Entry; }
}
}
[ProtoContract
, ProtoInclude(2, typeof(CaseRecord))
, ProtoInclude(3, typeof(UserRecord))
]
public interface IRecord: IEntry
{
}
public interface IEntry
{
long EntryId { get; set; }
long DataSourceId { get; set; }
}
public class UserRecord : RecordEntry<UserRecord>
{
// ...other datamembers...
[IgnoreDataMember]
public override long EntryId { get; set; }
[IgnoreDataMember]
public override long DataSourceId { get; set; }
}
[DataContract]
public abstract class RecordEntry<T> : RType<T>, IRecord
where T : RType<T>, new()
{
// other datamembers...
public abstract long EntryId { get; set; }
public abstract long DataSourceId { get; set; }
}
I(反)序列像這樣的記錄流:
Serializer.SerializeWithLengthPrefix(_stream, record, PrefixStyle.Base128);
Serializer.DeserializeWithLengthPrefix<Record>(_stream, PrefixStyle.Base128);
當我嘗試和反序列化,雖然,我Record
對象(這是類型UserRecord
)返回EntryId
和DataSourceId
的值爲0,我不完全確定爲什麼。我從Entry
和DataSource
id中刪除了[IgnoreDataMember]
,但我在deserilization時仍然得到0的值。對象的所有其他屬性都正確填充。
用戶記錄,像這樣創造了我的[TestMethod]
...
userRecord1 = new UserRecord()
{
// ...other datamembers
EntryId = 123456789,
DataSourceId = 123456789,
};