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();
If you're not sure:
IList<T> myList = nonGenericList.OfType<T>().ToList();
Of course, you will need the System.Linq namespace:
using System.Linq;
本文介绍在C#中如何使用泛型转换(Cast<T>() 和 OfType<T>() 方法)来操作非泛型集合,确保类型安全并获取所需类型的元素。文章详细解释了何时使用 Cast<T>() 和 OfType<T>() 方法,以及如何引入System.Linq命名空间以实现这一转换。
950

被折叠的 条评论
为什么被折叠?



