How do I convert a non-Generic IList to IList?

前端 未结 7 2009
失恋的感觉
失恋的感觉 2021-02-02 06:20

I have class that wants an IList, but I have a Systems.Collection.IList, coming from an NHibernate quere.

I want to create a method th

7条回答
  •  眼角桃花
    2021-02-02 06:41

    If you're sure that all of the elements inherit from T (or whatever type you're using)

    IList myList = nonGenericList.Cast().ToList();
    

    If you're not sure:

    IList myList = nonGenericList.OfType().ToList();
    

    Of course, you will need the System.Linq namespace:

    using System.Linq;
    

提交回复
热议问题