c# - 如何将非通用 IList 转换为 IList<T>?

标签 c#

我有一个类(class)想要 IList<T> , 但我有一个 Systems.Collection.IList , 来自 NHibernate 问题。

我想创建一个将其转换为 IList<T> 的方法.我该怎么做?

最佳答案

如果您确定所有元素都继承自 T(或您使用的任何类型)

IList<T> myList = nonGenericList.Cast<T>().ToList();

如果你不确定:

IList<T> myList = nonGenericList.OfType<T>().ToList();

当然,您将需要 System.Linq 命名空间:

using System.Linq;

关于c# - 如何将非通用 IList 转换为 IList<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/776397/

相关文章:

c# - 使用 Unity3d 时 Visual Studio 中的 IntelliSense

c# - 在 C# 中何时使用 ref 以及何时不需要

c# - DropDownList.SelectedValue 更改(作为 FormView 中的子控件)不固定

c# - Page head 元素中的尖括号

时间:2019-05-17 标签:c#dataGridViewnullreferenceException

c# - LINQ 到 Azure 搜索

c# - 构建不会在 asp.net mvc 的 View 中捕获错误

c# - 引用另一个项目?

c# - 从 Jquery 调用 WCF Restfull 服务

c# - 如何复制 Excel 绘制 "Scatter with smooth lines"图形的操作