如何在 C# 4.0 中检测对通用列表的添加?

发布于 2024-11-18 14:42:29 字数 723 浏览 3 评论 0原文

我有一个名为 LocationListList 子类。这是我们添加其他属性(例如 IsExpanded 等)的便捷方法,这样我们就可以在 UI 中使用。足够好了。但是现在,我们希望每个位置都知道其父位置。因此,当某些内容添加到 LocationList 时,我们需要收到通知,但您无法覆盖 AddRemove 虽然我们可以使用 ObservableCollection,但这更多的是 View/ViewModel。这是纯模型。 当然,

我们可以在将其添加到集合时手动设置父级,但我讨厌这样的非自动化逻辑,因为没有什么可以正确执行其设置,让集合(好吧,列表)自动说“嘿......你”。 '正在被添加,所以我会将你的父级设置为拥有我的类。”这就是我所追求的。

我的想法是代替子类 List 而只是创建一个使用直接类在内部列出,然后我可以简单地公开我自己的添加/删除方法并处理其中对“父级”的调整。但是,这样做会破坏枚举内部集合的能力,因为它位于内部。

也就是说,有没有什么方法可以监听 List 的更改,或者至少将其 IEnumerable 接口从包装对象委托给其包装器?

I have a subclass of List<Location> called LocationList. This is a convenient way for us to add other properties (like IsExpanded and such that we can use in the UI. Good enough. But now, we want each location to be aware of its parent. As such, we need to be notified when something is added to LocationList, but you can't override Add and Remove. While we can use ObservableCollection, that's more of a View/ViewModel construct. This is pure model data.

Sure we could manually set the parent when adding it to the collection, but I hate non-automated logic like that as there's nothing to enforce its set correctly. Letting the collection (well, List) automatically say 'Hey... you're being added, so I'll set your parent to the class that owns me." is what I'm after.

My thought is to instead of subclass List<Location> to instead just create a straight class that uses a List<Location> internally, and then I can simply expose my own Add/Remove methods and handle the adjustments to 'Parent' in there. However, doing so breaks being able to enumerate over the internal collection since it's inside.

That said, is there any way to either listen to changes to List<Location>, or at least delegate its IEnumerable interface from the wrapped object to its wrapper?

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

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

发布评论

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

评论(6

以为你会在 2024-11-25 14:42:29

将您的 LocationList 更改为从 Collection 继承。我不知道为什么 List 没有密封,但它不可扩展。

来源:框架设计指南,第二版,第 251 页:

List 针对性能和功耗进行了优化,但牺牲了 API 的简洁性和灵活性。

Change your LocationList to inherit from Collection<Location> instead. I don't know why List<T> isn't sealed, but it's not extendable.

Source: Framework Design Guidelines, Second Edition, Page 251:

List<T> is optimized for performance and power at the cost of cleanness of the APIs and flexibility.

秋日私语 2024-11-25 14:42:29

List 具有受保护的成员 InsertItemRemoveItem 等,您可以在派生类中重写这些成员以执行您的操作想。

** 更新 **

实际上上面是不正确的,它是 Collection 具有这些受保护的方法。一般来说,在派生自定义 List 类时,建议从 Collection 派生,而不是 List

请参阅这个答案

List<T> has protected members InsertItem<T>, RemoveItem<T> etc that you can override in your derived class to do what you want.

** UPDATE **

Actually the above is incorrect, it's Collection<T> that has these protected methods. In general, when deriving custom List classes, it is recommended to derive from Collection<T> rather than List<T>.

See this answer.

惜醉颜 2024-11-25 14:42:29

我的想法是代替子类
列出而不只是创建
一个直接类,使用
内部列出,然后我
可以简单地公开我自己的添加/删除
方法并进行调整
里面有‘家长’。然而,这样做
中断能够枚举
内部集合,因为它是
里面。

您仍然可以在自定义集合上实现 IEnumerable。使用List<>作为类的内部实现细节,并且您可以允许通过接口进行枚举。

class LocationList : IEnumerable<Location>
{
    List<Location> _list; // initialize somewhere

    public IEnumerator<Location> GetEnumerator() 
    {
         return _list.GetEnumerator();
    }

    IEnumerable.IEnumerator GetEnumerator() 
    {
         return this.GetEnumerator();
    }

    // ... your other custom properties and methods
}

My thought is to instead of subclass
List to instead just create
a straight class that uses a
List internally, and then I
can simply expose my own Add/Remove
methods and handle the adjustments to
'Parent' in there. However, doing so
breaks being able to enumerate over
the internal collection since it's
inside.

You can still implement IEnumerable<Location> on your custom collection. Use the List<> as the internal implementation detail of the class, and you can allow enumeration via the interface.

class LocationList : IEnumerable<Location>
{
    List<Location> _list; // initialize somewhere

    public IEnumerator<Location> GetEnumerator() 
    {
         return _list.GetEnumerator();
    }

    IEnumerable.IEnumerator GetEnumerator() 
    {
         return this.GetEnumerator();
    }

    // ... your other custom properties and methods
}
平定天下 2024-11-25 14:42:29

尝试通用的 BindingList,添加和删​​除项目时会引发事件。

Try the generic BindingList<T>, which raises events when items are added and removed.

末骤雨初歇 2024-11-25 14:42:29

但是,这样做会破坏枚举内部集合的能力,因为它位于内部。

假设您遵循正确的技术并实现 IEnumerable 和 ICollection ,情况并非如此

,但我会使用 Observable 集合而不是重新发明轮子。

当然,您可以使用反应式扩展来完成您需要的操作。

However, doing so breaks being able to enumerate over the internal collection since it's inside.

This is not true assuming you follow proper technique and implement IEnumerable and ICollection

But I would use Observable collection instead of reinventing the wheel.

And of course you can use Reactive extensions to do what you need.

゛时过境迁 2024-11-25 14:42:29

您可以使用 观察者模式

You can either using Observer pattern

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