使用 List反序列化泛型类

发布于 2024-12-12 05:11:27 字数 1905 浏览 0 评论 0原文

我使用 XMLSerialiser 反序列化我的通用类时遇到问题。 这是一些代码

[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 但这是代码中使用的结构,它在我们使用的所有其他代码中就像一个魅力,所以我们会保持一致。

I have a problem with a generic class of mine that I am deserializing with the XMLSerialiser.
Here is some code

[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

}

Did anybody encounter a similar behavior. The funny thing is that the object (that is added to the above List) is correctly filled with the appropriate data from the XML stream we are processing.

It may be worth showing the class that implements the above class, which is itself also a generic class

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

    #region properties

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

    #endregion
}

And for completeness here the class in the list

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

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

    #endregion

}

I am well aware of XMLArray and XMLArrayItem but this is the structure that has been used in the code and it works like a charm in all the other code we use so we would to remain consistent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北凤男飞 2024-12-19 05:11:27

尝试改用 XmlArray 属性:

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

Try using the XmlArray attribute instead:

[XmlArray("List")]
public List<T> MyList { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文