2011-10-24 83 views
2

我用XMLSerialiser反序列化我的泛型類時遇到了問題。 這裏是一些代碼使用列表反序列化泛型類<T>

[XmlInclude(typeof(OrderProposalsProductGroupData<OrderProposalsProductGroupProposals>))] 
public class OrderProposalsStoreProductGroups<TU> : OrderProposalsStore<TU> where TU : class 
{ 

    #region properties 

    /// <summary> 
    /// TODO 
    /// </summary> 
    [XmlElement("groups")] 
    /* BUG: This list is not fully filled during deserialisation. Only one entry is added although the stream does definetly have more entries. Why the hell? It works with all other classes in our logic but not here. 
    * Maybe the deserializer has problems with the generic nature? but I couldn't find any such issue reported anywhere in the internet or any 
    restriction description concerning generics (in fact I found a bunch of examples using generics with the Deserialiser). Actually the MS XMLSerializer description confirms 
    that any class implementing an IEnumerable or ICollection can be deserialized so it makes no sense it doesn't work. Anybody a clue? */ 
    public List<TU> ProductGroups { get; set; } 

    #endregion 

} 

有沒有人遇到類似的行爲。有趣的是,對象(即添加到上面的列表中)正確填充了我們正在處理的XML流中的適當數據。

這可能是值得炫耀實現上述類的類,它本身也是一個通用類

public class OrderProposalsStores<T> : EntityBase where T : new() 
{ 

    #region properties 

    [XmlElement("Store")] 
    public T OrderProposalsStore 
    { 
     get; 
     set; 
    } 

    #endregion 
} 

以及物品是否完整在這裏的列表中的類

[Serializable, 
XmlInclude(typeof(OrderProposalsProductGroupProposals))] 
public class OrderProposalsProductGroupData<TU> : EntityBase where TU : OrderProposalsProductGroup 
{ 
    #region properties 

    [XmlElement("productgroup")] 
    public TU ProductGroup { get; set; } 

    #endregion 

} 

我很好意識到XMLArray和XMLArrayItem,但這是代碼中使用的結構,它在我們使用的所有其他代碼中都像一個魅力一樣,所以我們將保持一致(好,並且截止日期與我接近......) 。無論如何,這是我的意見,它應該以任何方式工作,我不知道爲什麼它不。

我很欣賞任何輸入或幫助。

在此先感謝

回答

1

嘗試使用XmlArray屬性改爲:

[XmlArray("List")] 
public List<T> MyList { get; set; } 
+0

與XmlArray簡單地更換不起作用!這一點完全停止了反序列化。 –