[英]DataContractSerializer not serializing all properties of a custom object within collection
我有以下課程:
[Serializable]
[DataContract(Name = "LayerInfo", Namespace = "ems.gis")]
public abstract class LayerPersistInfo
{
public LayerPersistInfo() { }
public LayerPersistInfo(int index, MappingContextBase context)
{
Index = index;
Context = context;
}
[DataMember(Name="idx", Order=0)]
public int Index { get; set; }
//[DataMember(Name = "name")]
//public string Name { get; set; }
[DataMember(EmitDefaultValue = true, Name="ctx", Order=1)]
public MappingContextBase Context { get; set; }
[DataMember(EmitDefaultValue = false, Name="lyrs", Order=2)]
public LayersPersistInfo Children { get; set; }
public abstract TocItemModel GetLayerModel();
}
[Serializable]
[CollectionDataContract(Name = "lyrs", Namespace = "ems.gis", ItemName = "lyr")]
public class LayersPersistInfo : List<LayerPersistInfo>
{
}
抽象LayerPersistInfo類的具體實現的多個實例最終出現在我需要序列化的LayersPersistInfo中。 我的觀察很奇怪。
如果LayersPersistInfo有2個或更多帶子項,則索引為0的元素的每個子項的Context屬性為null。 在重新填充集合之后的下一次序列化嘗試中,LayersPersistInfo的索引1處的元素的每個子元素的Context屬性為null。 在下一次嘗試中,項目0的子級都具有空Context,依此類推。 此行為非常一致。
如果我的自定義集合LayersPersistInfo只有一項,則所有子項均已正確序列化。
我在序列化程序實例上調用WriteObject之前放置了一個斷點,並且這些屬性永遠不會為null。 我在這里可能做錯了什么?
TIA。
我的錯。 反序列化數據后未正確使用Context屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.