How can I expose only a fragment of IList<>?

痴心易碎 提交于 2019-12-03 17:22:57

问题


I have a class property exposing an internal IList<> through

System.Collections.ObjectModel.ReadOnlyCollection<>

How can I pass a part of this ReadOnlyCollection<> without copying elements into a new array (I need a live view, and the target device is short on memory)? I'm targetting Compact Framework 2.0.


回答1:


Try a method that returns an enumeration using yield:

IEnumerable<T> FilterCollection<T>( ReadOnlyCollection<T> input ) {
    foreach ( T item in input )
        if (  /* criterion is met */ )
            yield return item;
}



回答2:


These foreach samples are fine, though you can make them much more terse if you're using .NET 3.5 and LINQ:

return FullList.Where(i => IsItemInPartialList(i)).ToList();



回答3:


You can always write a class that implements IList and forwards all calls to the original list (so it doesn't have it's own copy of the data) after translating the indexes.




回答4:


You could use yield return to create a filtered list

IEnumerable<object> FilteredList()
{
    foreach( object item in FullList )
    {
        if( IsItemInPartialList( item )
            yield return item;
    }
}



回答5:


Depending on how you need to filter the collection, you may want to create a class that implements IList (or IEnumerable, if that works for you) but that mucks about with the indexing and access to only return the values you want. For example

class EvenList: IList
{
    private IList innerList;
    public EvenList(IList innerList)
    {
         this.innerList = innerList;
    }

    public object this[int index]
    {
        get { return innerList[2*i]; }
        set { innerList[2*i] = value; }
    }
    // and similarly for the other IList methods
}



回答6:


How do the filtered elements need to be accessed? If it's through an Iterator then maybe you could write a custom iterator that skips the elements you don't want publicly visible?

If you need to provide a Collection then you might need to write your own Collection class, which just proxies to the underlying Collection, but prevents access to the elements you don't want publicly visible.

(Disclaimer: I'm not very familiar with C#, so these are general answers. There may be more specific answers to C# that work better)



来源:https://stackoverflow.com/questions/39447/how-can-i-expose-only-a-fragment-of-ilist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!