C# 如何将非通用 IList 转换为 IList<T>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/776397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert a non-Generic IList to IList<T>?
提问by Malcolm
I have class that wants an IList<T>
, but I have a Systems.Collection.IList
, coming from an NHibernate quere.
我有一个需要 的类IList<T>
,但我有一个Systems.Collection.IList
来自 NHibernate quere 的类。
I want to create a method that converts it to an IList<T>
. How do I do this?
我想创建一个将其转换为IList<T>
. 我该怎么做呢?
回答by Frederik Gheysels
Can't you achieve that NHibernate returns you an IList ?
The ICriteria interface defines an List<T>
method ...
你不能实现 NHibernate 返回一个 IList 吗?ICriteria 接口定义了一个List<T>
方法......
回答by Tamas Czinege
If you're sure that all of the elements inherit from T (or whatever type you're using)
如果您确定所有元素都继承自 T(或您使用的任何类型)
IList<T> myList = nonGenericList.Cast<T>().ToList();
If you're not sure:
如果您不确定:
IList<T> myList = nonGenericList.OfType<T>().ToList();
Of course, you will need the System.Linq namespace:
当然,您将需要 System.Linq 命名空间:
using System.Linq;
回答by Miha Markic
One way is to use Linq, like list.OfType<T>()
or .Cast<T>()
一种方法是使用 Linq,例如list.OfType<T>()
或.Cast<T>()
回答by Scott Wisniewski
Cast
and OfType
return IEnumerable<T>
implemetnations, not IList<T>
implementations, so they wouldn't be useful to you.
Cast
并OfType
返回IEnumerable<T>
实现,而不是IList<T>
实现,因此它们对您没有用。
Calling .Cast<T>().ToList
will result in an extra copy of the list, which may have adverse performance implications.
打电话。Cast<T>().ToList
将导致列表的额外副本,这可能会对性能产生不利影响。
A better (IMHO) approach would be to just create a wrapper class and do the conversion on the fly. You want something like this:
更好的(恕我直言)方法是创建一个包装类并即时进行转换。你想要这样的东西:
class ListWrapper<T> : IList<T>
{
private IList m_wrapped;
//implement all the IList<T> methods ontop of m_wrapped, doing casts
//or throwing not supported exceptions where appropriate.
//You can implement 'GetEnumerator()' using iterators. See the docs for
//yield return for details
}
That will have the advantage of not create another copy of the entire list.
这将具有不创建整个列表的另一个副本的优点。
回答by mako
Frederik is correct, NHibernate already does this.
Frederik 是对的,NHibernate 已经这样做了。
var data = query.List<MyType>();
This would result in an IList of the type you specified. In this case IList<MyType>
.
这将导致您指定类型的 IList。在这种情况下IList<MyType>
。
回答by Ram Karri
Fredrick is correct. But Nhibernate also has an option to return multiple result sets. For example:
弗雷德里克是对的。但是 Nhibernate 也有返回多个结果集的选项。例如:
IList multiResults = session.CreateMultiCriteria()
.Add(pageCriteria)
.Add(countCriteria)
.List();
In the above call there isn't an option to return a typed list. So it has to be casted as mentioned above, like this:
在上面的调用中,没有返回类型列表的选项。所以它必须像上面提到的那样进行铸造,像这样:
IList<T> results = ((IList)multiResults[0]).Cast<T>().ToList();
IList counts = (IList)multiResults[1];
回答by nnunes10
No need for use Linq in this case (OfType() or .Cast). NHibernate implementation is enough.
在这种情况下不需要使用 Linq(OfType() 或 .Cast)。NHibernate 实现就足够了。
You just have to use:
你只需要使用:
var results= query.List<YourType>();
Instead of
代替
var results= query.List<>();