DataContractSerializer not serializing all properties of a custom object within collection
I have the following classes:
[Serializable]
[DataContract(Name = "LayerInfo", Namespace = "ems.gis")]
public abstract class LayerPersistInfo
{
public 开发者_如何学JAVALayerPersistInfo() { }
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>
{
}
Multiple instances of concrete implementations of the abstract LayerPersistInfo class end up in LayersPersistInfo which I need to serialize. What I am observing is very strange.
If LayersPersistInfo has 2 or more items with children, the Context property of each child of the element at index 0 is null. On the next serialization attempt after repopulating the collection, Context property of each child of the element at index 1 of LayersPersistInfo is null . On the next attempt, children of item 0 all have null Context and so on . This behaviour is very consistent.
If my custom collection LayersPersistInfo has just one item, all children are properly serialized.
I have put a break point just before calling WriteObject on the serializer instance and these property is never null. What could I possibly be doing wrong here?
TIA.
My fault. Was not using Context property properly after data was deserialized.
精彩评论