简体   繁体   English

如何将通用List <T>转换为List?

[英]How can I convert a generic List<T> to a List?

How can I convert a generic List to a List? 如何将通用列表转换为列表?

I am using a ListCollectionView and I need to provide it with an IList not an IList<T> . 我正在使用ListCollectionView ,我需要为它提供一个IList而不是IList<T>

I see plenty of examples to convert an IList to a IList<T> , but not the other way. 我看到很多例子可以将IList转换为IList<T> ,但不是另一种方式。

Do I just convert it manually ( new List().AddRange(IList<T> )? 我只是手动转换它( new List().AddRange(IList<T> )?

To clarify the other answers: 澄清其他答案:

IList<T> does not require an implementing type to also implement IList . IList<T> 不需要实施类型也实现IList IEnumerable<T> does require IEnumerable . IEnumerable<T>确实需要IEnumerable We can get away with that with IEnumerable<T> because a sequence of T can always be treated as a sequence of objects. 我们可以通过IEnumerable<T>来解决这个问题,因为IEnumerable<T>的序列总是可以被视为一系列对象。 But a list of giraffes cannot be treated as a list of objects; 但是长颈鹿的名单不能被视为对象列表; you can add a tiger to a list of objects. 您可以将老虎添加到对象列表中。

However, List<T> does unsafely implement IList . 但是, List<T>不安全地实现IList If you try to add a tiger to a List<Giraffe> by first casting it to IList, you'll get an exception. 如果您尝试将老虎添加到List<Giraffe> ,首先将其投射到IList,您将获得异常。

So to answer the question: If all you have in hand is an IList<T> , you can speculatively cast it to IList with the "as" operator, and if that fails, then create a new ArrayList or List<object> and copy the contents in. If what you have in hand is a List<T> then you already have something that implements IList directly. 所以回答这个问题:如果您拥有的只是一个IList<T> ,您可以推测性地使用“as”运算符将其IListIList ,如果失败,则创建一个新的ArrayListList<object>并复制如果您拥有的是List<T>那么您已经拥有了直接实现IList东西。

As List<T> implements the IList interface, you can simply provide the List<T> . 由于List<T>实现IList接口,您只需提供List<T> If needed, just cast it: 如果需要,只需投下它:

List<int> list = new List<int>();
IList ilist = (IList)list;

You don't have to do any conversion at all. 您根本不需要进行任何转换。 A List<T> implements the IList interface. List<T>实现IList接口。

If you need the type IList for example to use a specific overload, just use the cast syntax (IList)theList . 如果您需要类型IList例如使用特定的重载,只需使用(IList)theList语法(IList)theList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将通用列表转换为dataTable? - How can i Convert Generic List into dataTable? 如何从任务转换<generic.list<t> &gt; 到一个 Generic.IEnumerable<t> ? </t></generic.list<t> - How can I convert from a Task<Generic.List<T>> to a Generic.IEnumerable<T>? 无法将一个通用列表转换为另一个 - Can't convert one generic list to another 我该如何修复无法将 Generic.List&lt;&gt; 隐式转换为 Generic.List&lt;&gt; - How can i fix Cannot implicitly convert Generic.List< > to Generic.List< > 如何转换通用列表 <T> 到基于接口的列表 <T> - How to convert a generic List<T> to an Interface based List<T> 如何将联合的通用列表转换为通用列表? - How to convert unioned generic list to generic list? 如何将 SqlDataReader 结果转换为通用列表 List<T> - How to convert SqlDataReader result to generic list List<T> 将通用列表转换为BindingList <T> - Convert generic list to BindingList<T> 我如何转换 &#39;System.Collection.Generic.List<String> &#39; 到 &#39;System.Windows.Documents.List&#39;? - How can I convert 'System.Collection.Generic.List<String>' into 'System.Windows.Documents.List'? 我如何返回通用列表 <T> 从通用方法 <T> () - How can i return a generic List<T> from a generic method<T>()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM