【问题标题】:DataContractSerializer not serializing all properties of a custom object within collectionDataContractSerializer 未序列化集合中自定义对象的所有属性
【发布时间】:2010-05-18 03:42:20
【问题描述】:

我有以下课程:

[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 属性为空。在重新填充集合后的下一次序列化尝试中,LayersPersistInfo 索引 1 处元素的每个子元素的 Context 属性为 null 。在下一次尝试中,项目 0 的子项都具有 null Context 等等。这种行为非常一致。

如果我的自定义集合 LayersPersistInfo 只有一项,则所有子项都已正确序列化。

我在序列化程序实例上调用 WriteObject 之前设置了一个断点,这些属性永远不会为空。我在这里可能做错了什么?

TIA。

【问题讨论】:

    标签: c# datacontractserializer


    【解决方案1】:

    我的错。数据反序列化后未正确使用 Context 属性。

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 2011-03-10
      相关资源
      最近更新 更多