数组列表列出< T>?

数组列表列出< T>?

问题描述:

我怎么可以转换/转储一个ArrayList到一个列表?我使用ArrayList的,因为我使用的是ASP.NET配置文件功能,它看上去像一个痛苦的列表存储在配置文件中。

How can I convert/dump an arraylist into a list? I'm using arraylist because I'm using the ASP.NET profiles feature and it looked like a pain to store List in profiles.

请注意: 另一种选择是将包裹列到自己的类和ArrayList的废除。

Note: The other option would be to wrap the List into an own class and do away with ArrayList.

http://www.i$p$pferjim.com/site/2009/04/storing-generics-in-asp-net-profile-object/

要转换的的ArrayList 全类型的对象 T 会是这样(假设你使用.NET 3.5或更高版本):

The easiest way to convert an ArrayList full of objects of type T would be this (assuming you're using .NET 3.5 or greater):

List<T> list = arrayList.Cast<T>().ToList();

如果您使用的是3.0或更早版本,你必须自己循环:

If you're using 3.0 or earlier, you'll have to loop yourself:

List<T> list = new List<T>(arrayList.Count);

foreach(T item in arrayList) list.Add(item);