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

前端 未结 7 1825
失恋的感觉
失恋的感觉 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:48

    Cast and OfType return IEnumerable implemetnations, not IList implementations, so they wouldn't be useful to you.

    Calling .Cast().ToList will result in an extra copy of the list, which may have adverse performance implications.

    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 : IList
    {
        private IList m_wrapped;
    
        //implement all the IList 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.

提交回复
热议问题