I was trying to implement a specialized collection that works like ObservableCollection
to encapsulate some more mechanisms in it, to do that i also let my collection inherit from Collection
and i also implement the same interfaces.
我试图实现一个像ObservableCollection一样的专用集合来封装更多的机制,为此我还让我的集合继承自Collection,我也实现了相同的接口。
I just do not get though how one actually implements the whole collection-changed-logic, for example Collection<T>.Add
is not being overridden (it is not even marked as virtual), so how does the ObservableCollection
fire the CollectionChanged
event if items were added using that method?
我只是不知道如何实际实现整个集合 - 更改 - 逻辑,例如Collection
3 个解决方案
#1
10
To answer your specific question, Collection<T>.Add
calls the InsertItem
virtual method (after checking that the collection is not read-only). ObservableCollection<T>
indeed overrides this method to do the insert and raise the relevant change notifications.
为了回答您的具体问题,Collection
#2
8
It does so by calling InsertItem
which is overridden and can be seen upon decompilation
它通过调用被重写的InsertItem来实现,并且可以在反编译时看到
protected override void InsertItem(int index, T item)
{
this.CheckReentrancy();
base.InsertItem(index, item);
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
}
#3
0
Remember, the key is not in overriding the base Collection methods, it's in the fact that you will be implementing the ICollection interface. And frankly, rather than inheriting from a Collection class, I would suggest instead creating an adapter class that takes a ICollection in the constructor and your methods will just delegate to the inner collection and raise the appropriate events.
请记住,关键不在于覆盖基本的Collection方法,而是在于您将实现ICollection接口。坦率地说,我建议改为创建一个在构造函数中接受ICollection的适配器类,而不是从Collection类继承,您的方法将委托给内部集合并引发相应的事件。
#1
10
To answer your specific question, Collection<T>.Add
calls the InsertItem
virtual method (after checking that the collection is not read-only). ObservableCollection<T>
indeed overrides this method to do the insert and raise the relevant change notifications.
为了回答您的具体问题,Collection
#2
8
It does so by calling InsertItem
which is overridden and can be seen upon decompilation
它通过调用被重写的InsertItem来实现,并且可以在反编译时看到
protected override void InsertItem(int index, T item)
{
this.CheckReentrancy();
base.InsertItem(index, item);
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
}
#3
0
Remember, the key is not in overriding the base Collection methods, it's in the fact that you will be implementing the ICollection interface. And frankly, rather than inheriting from a Collection class, I would suggest instead creating an adapter class that takes a ICollection in the constructor and your methods will just delegate to the inner collection and raise the appropriate events.
请记住,关键不在于覆盖基本的Collection方法,而是在于您将实现ICollection接口。坦率地说,我建议改为创建一个在构造函数中接受ICollection的适配器类,而不是从Collection类继承,您的方法将委托给内部集合并引发相应的事件。