使用 List反序列化泛型类
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试改用
XmlArray
属性:Try using the
XmlArray
attribute instead: