序列化为XML ArrayList< T>。 .NET Core 3中 [英] Serializing to XML ArrayList<T> in .NET Core 3

查看:68
本文介绍了序列化为XML ArrayList< T>。 .NET Core 3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以从.net core 3中的控制器(其中 ArrayList )序列化 ActionResult< ArrayList> 由某些类型的 T (在这种情况下为 Person )组成。

Is there a way to serialize ActionResult<ArrayList> from a controller in .net core 3, where ArrayList is composed of some type T (Person in this case).

它不会序列化为XML,只能序列化为JSON。它抱怨类型 Person 是未知的(即使它是已知的,并且它本身也可以作为数组进行序列化)。

It won't serialize to XML, only JSON. It complains the type Person is not known (even though it is, and it serializes just fine itself or as an array).

ie这会导致序列化失败:

i.e. this fails serialization:

[HttpGet("List")]
public ActionResult<ArrayList> AllPersons() {...}

此方法有效:

[HttpGet("List")]
public ActionResult<Person[]> AllPersons() {...}

所以 Person 类型( T )本身可以很好地序列化,而 Person [] 也可以序列化,但是当 ArrayList (<人的人)在XML序列化失败时出现以下情况:

So the Person type (T) can serialize just fine by itself, and Person[] also serializes just fine, but when an ArrayList (of Person) fails XML serialization with:


System.InvalidOperationException:生成
XML文档时出错。 ---> System.InvalidOperationException:不应预期类型
SimpleRESTServer.Models.Person。使用XmlInclude或
SoapInclude属性来指定静态未知的类型。

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type SimpleRESTServer.Models.Person was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

Person 类型是静态已知的,并且 Person Person [] 都可以序列化) :

(Person type is known statically, and both Person and Person[] will serialize just fine):

[XmlInclude(typeof(Person))]
[Serializable]
public class Person ...


推荐答案

ArrayList 不推荐使用了很长一段时间。正如@MindingData 在评论中提及 ArrayList 不是通用的。

ArrayList has been deprecated for quite a while. As @MindingData mentioned in a comment, ArrayList isn't generic.

您应该考虑使用 List< Person> 而不是 ArrayList 。我大概十年都没见过使用 ArrayList 了-多数情况下大多数序列化框架都不会优雅地处理它,至少部分原因是因为它既没有实现 IEnumerable< T> ICollection< T> 。迁移到较新的通用集合类型可能会解决您在处理较旧的,已弃用的非通用集合类型时遇到的所有奇怪错误。

You should look into using List<Person> instead of ArrayList. I haven't seen ArrayList used in probably about a decade--chances are most serialization frameworks aren't going to handle it gracefully, at least in part because it implements neither IEnumerable<T> nor ICollection<T>. Migrating to newer, generic collection types will probably resolve any strange errors you encounter while dealing with older, deprecated, non-generic collection types.

您也许可以进行测试通过在测试用例中使用 object [] 而不是 Person [] 来实现理论。 ArrayList 更接近 object [] ,而 List< Person> 接近 Person [] 。 (也就是说,某些序列化框架即使无法处理 ArrayList 也能正确处理 object [] ,所以这不是一个完美的测试。)

You may be able to test this theory by using object[] instead of Person[] in your test case. ArrayList is closer to object[], whereas List<Person> is closer to Person[]. (That being said, some serialization frameworks will handle object[] correctly even when they're unable to handle ArrayList, so this isn't a perfect test.)

这篇关于序列化为XML ArrayList&lt; T&gt;。 .NET Core 3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆