我如何转换为非通用的IList到IList< T> ;? [英] How do I convert a non-Generic IList to IList<T>?
本文介绍了我如何转换为非通用的IList到IList< T> ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有类,希望有一个的IList< T>
,但我有一个 Systems.Collection.IList
,从一个NHibernate的队列中去未来
I have class that wants an IList<T>
, but I have a Systems.Collection.IList
, coming from an NHibernate quere.
我想创建将其转换为的IList℃的方法; T>
。我该怎么做呢?
I want to create a method that converts it to an IList<T>
. How do I do this?
推荐答案
如果你确定所有的元素从T(或任何类型的你继承重新使用)
If you're sure that all of the elements inherit from T (or whatever type you're using)
IList<T> myList = nonGenericList.Cast<T>().ToList();
如果你不知道:
IList<T> myList = nonGenericList.OfType<T>().ToList();
当然,你将需要System.Linq的命名空间:
Of course, you will need the System.Linq namespace:
using System.Linq;
这篇关于我如何转换为非通用的IList到IList< T> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文