当我在 VS 2008 中看到 ReadOnlyObservableCollection 的元数据时,我感到非常惊讶......
public class ReadOnlyObservableCollection<T> : ReadOnlyCollection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
// Summary:
// Initializes a new instance of the System.Collections.ObjectModel.ReadOnlyObservableCollection<T>
// class that serves as a wrapper for the specified System.Collections.ObjectModel.ObservableCollection<T>.
//
// Parameters:
// list:
// The collection to wrap.
public ReadOnlyObservableCollection(ObservableCollection<T> list);
// Summary:
// Occurs when an item is added or removed.
protected virtual event NotifyCollectionChangedEventHandler CollectionChanged;
//
// Summary:
// Occurs when a property value changes.
protected virtual event PropertyChangedEventHandler PropertyChanged;
// Summary:
// Raises the System.Collections.ObjectModel.ReadOnlyObservableCollection<T>.CollectionChanged
// event.
//
// Parameters:
// args:
// The event data.
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args);
//
// Summary:
// Raises the System.Collections.ObjectModel.ReadOnlyObservableCollection<T>.PropertyChanged
// event.
//
// Parameters:
// args:
// The event data.
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args);
}
如您所见,CollectionChanged(INotifyCollectionChanged 的成员)是在 protected 中实现的...而我无法在自己的类中执行此操作。
.NET 框架不应编译!
有人能解释一下这个谜团吗?
最佳答案
如果您仔细检查 ReadOnlyObservableCollection,您会发现它显式实现了 INotifyPropertyChanged。换句话说, protected 事件处理程序并不是接口(interface)的真正实现 - INotifyCollectionChanged.CollectionChanged是。
关于c# - 如何在 protected 中实现接口(interface)成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2476299/