c# - 如何在 protected 中实现接口(interface)成员?

标签 c# .net

当我在 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/

相关文章:

javascript - 将 html.actionLink 添加到 Jquery 数据表

c# - 什么应该进入顶级命名空间?

c# - 显示按日期排序的数据

c# - WPF:将虚拟文件拖放到 Windows 资源管理器中

c# - 将 Ninject v2 与 Asp.net Webforms 集成时出现问题

c# - Automapper 能否将复杂的源图映射到目标属性中没有前缀且没有自定义映射的平面目标?

c# - 在使用中包装 MemoryStream

c# - 为什么我的流在我自己解锁之前就被解锁了?

.net - .NET 中的包装类型 : struct or class?

c# - 从 C# 代码调用 delphi DLL 方法