如何仅公开 IList<> 的一个片段?

发布于 2024-07-04 19:55:35 字数 248 浏览 6 评论 0 原文

我有一个公开内部 IList<> 的类属性 通过

System.Collections.ObjectModel.ReadOnlyCollection<>

如何在不将元素复制到新数组的情况下传递此 ReadOnlyCollection<> 的一部分(我需要实时视图,并且目标设备内存不足)? 我的目标是 Compact Framework 2.0。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最终幸福 2024-07-11 19:55:35

需要如何访问过滤后的元素? 如果是通过迭代器,那么也许您可以编写一个自定义迭代器来跳过您不希望公开可见的元素?

如果您需要提供 Collection,那么您可能需要编写自己的 Collection 类,该类仅代理底层 Collection,但阻止访问您不希望公开可见的元素。

(免责声明:我对 C# 不太熟悉,所以这些是一般答案。可能有更具体的 C# 答案,效果更好)

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)

何处潇湘 2024-07-11 19:55:35

您可以使用yield return来创建过滤列表

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

You could use yield return to create a filtered list

IEnumerable<object> FilteredList()
{
    foreach( object item in FullList )
    {
        if( IsItemInPartialList( item )
            yield return item;
    }
}
甜尕妞 2024-07-11 19:55:35

根据您需要如何过滤集合,您可能想要创建一个实现 IList(或 IEnumerable,如果这适合您)的类,但这会影响索引和访问,仅返回您想要的值。 例如

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
}

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
}
玻璃人 2024-07-11 19:55:35

尝试使用yield 返回枚举的方法:

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

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;
}
红尘作伴 2024-07-11 19:55:35

这些 foreach 示例很好,但如果您使用 .NET 3.5 和 LINQ,则可以使它们更加简洁:

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

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();
过期以后 2024-07-11 19:55:35

您始终可以编写一个实现 IList 的类,并在转换索引后将所有调用转发到原始列表(因此它没有自己的数据副本)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文