如何在我的实现IEnumerable 的Dictionary包装器类中实现IEnumerable?

我正在尝试为Dictionary创建一个包装器。

Dictionary实现IEnumerable<KeyValuePair> ,但我希望我的包装类实现IEnumerable 。 所以我尝试了这个:

 public class FooCollection : IEnumerable { private Dictionary fooDictionary = new Dictionary(); public IEnumerator GetEnumerator() { return fooDictionary.Values.GetEnumerator(); } // Other wrapper methods omitted } 

但是我收到此错误:

‘FooCollection’没有实现接口成员’System.Collections.IEnumerable.GetEnumerator()’。 ‘FooCollection.GetEnumerator()’无法实现’System.Collections.IEnumerable.GetEnumerator()’,因为它没有匹配的返回类型’System.Collections.IEnumerator’。

但是我不明白这个错误,因为FooCollection.GetEnumerator()返回一个IEnumerator ,而IEnumerator是一个IEnumerator

编辑:

显式实现IEnumerator.GetEnumerator()的解决方案有效。 但是我现在想知道为什么当我在List上“去定义”时,我只看到GetEnumerator的一个定义: public List.Enumerator GetEnumerator();

显然List可以有一个GetEnumerator方法,它返回实现IEnumeratorIEnumerator ,但我必须为每个方法都有一个方法?

编辑:

正如下面LukeH所回答的, List 确实包含了显式接口实现。 显然,Visual Studio在从元数据生成方法存根时不会列出那些。 (请参阅前一个问题: 为什么VS元数据视图不显示显式接口实现的成员 )

在发布此问题之前,我曾尝试检查List (通过Visual Studio中的“转到定义”)以查看是否需要实现多个版本的GetEnumerator。 我想这不是最可靠的检查方式。

无论如何,我将此标记为已回答。 谢谢你的帮助。

添加以下显式接口实现:

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

虽然IEnumerator是一个IEnumerator ,但是IEnumerator的合同专门返回一个IEnumerator ,而不是一个IEnumerator

实现IEnumerable ,还必须显式实现IEnumerable.GetEnumerator() 。 generics接口的方法本身作为非generics方法契约的实现无效。 你可以有一个调用另一个,或者因为你有一个你正在使用的枚举器的子对象,只需复制/粘贴;

 using System.Collections; using System.Collections.Generic; public class FooCollection : IEnumerable { private Dictionary fooDictionary = new Dictionary(); public IEnumerator GetEnumerator() { return fooDictionary.Values.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { //forces use of the non-generic implementation on the Values collection return ((IEnumerable)fooDictionary.Values).GetEnumerator(); } // Other wrapper methods omitted } 

问题是.NET中没有返回类型协方差这样的东西 – IEnumerator M()IEnumerator M()是完全不同的方法。

解决方法是您必须显式实现非generics版本:

 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { // this calls the IEnumerator GetEnumerator method // as explicit method implementations aren't used for method resolution in C# // polymorphism (IEnumerator implements IEnumerator) // ensures this is type-safe return GetEnumerator(); } 

只要通用IEnumerable {T}inheritanceIEnumerable你也必须实现IEnumerable.GetEnumerator() 。 您可以明确地执行以下操作:

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

你已经有了几个主要问题的答案。 我会回答你编辑中提出的问题……

List类实际上有三个不同的GetEnumerator方法:当编译时实例被输入为List本身时调用的公共方法,以及两个用于满足IEnumerable / IEnumerable契约的显式接口实现。 所有三种方法返回的枚举器对象都是幕后的List.Enumerator类型。

 // Public method public List.Enumerator GetEnumerator() { /* ... */ } // IEnumerable explicit interface implementation IEnumerator IEnumerable.GetEnumerator() { /* ... */ } // IEnumerable explicit interface implementation IEnumerator IEnumerable.GetEnumerator() { /* ... */ } 

实现通用IEnumerable接口时,还必须实现非通用IEnumerable接口。 错误是关于缺少的非generics方法。

这是IEnumerable的声明:

 public interface IEnumerable : IEnumerable { new IEnumerator GetEnumerator(); } 

注意新的关键字。

这是IEnumerable的声明:

 public interface IEnumerable { IEnumerator GetEnumerator(); } 

所以现在你有一个GetEnumerator方法,但是你实现了哪两个呢? 因此,您需要添加非generics版本的显式实现:

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