【发布时间】:2014-03-26 16:30:49
【问题描述】:
首先,这个问题与How to call an explicitly implemented interface-method on the base class 非常相似,但又不一样。
我的问题是,我什至无法向相关基类添加任何辅助方法来解决该问题中解决的问题。
代码:
class AsyncObservableCollection<T> : ObservableCollection<T>
{
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new ThreadSafeEnumerator<T>(this.GetEnumerator(), this.mutex);
}
}
其中ObservableCollection<T> 来自Collection<T>,它实现了IEnumerable<T>。
编译错误:
AsyncObservableCollection<T>.IEnumerable<...>.GetEnumerator()':
containing type does not implement interface 'System.Collections.Generic.IEnumerable<T>
问题:
所以我的目标是让任何 foreach 循环使用我的 AsyncObservableCollection<T> 的实例来获取我的自定义枚举器,同时在我的自定义方法中允许 this.GetEnumerator() 仍然获得“标准”枚举器。我不确定最好的方法是什么。
【问题讨论】:
标签: c# interface overriding base enumerator