![](/img/trans.png)
[英]How to Implement GetEnumerator method for class that implements IEnumerable<IEnumerable<T>>
[英]How do I implement IEnumerable in my Dictionary wrapper class that implements IEnumerable<Foo>?
我正在尝试为Dictionary<String,Foo>
创建一个包装器。
Dictionary<String,Foo>
实现IEnumerable<KeyValuePair<String,Foo>>
,但我希望我的包装类实现IEnumerable<Foo>
。 所以我尝试了这个:
public class FooCollection : IEnumerable<Foo>
{
private Dictionary<string, Foo> fooDictionary = new Dictionary<string, Foo>();
public IEnumerator<Foo> 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<Foo>
,而IEnumerator<Foo>
是一个IEnumerator
。
编辑:
显式实现IEnumerator.GetEnumerator()
的解决方案有效。 但是我现在想知道为什么当我在List<T>
上“去定义”时,我只看到GetEnumerator的一个定义: public List<T>.Enumerator GetEnumerator();
显然List<T>
可以有一个GetEnumerator
方法,它返回实现IEnumerator<T>
和IEnumerator
,但是我必须有一个方法?
编辑:
正如下面的LukeH所回答的, List<T>
确实包含了显式接口实现。 显然,Visual Studio在从元数据生成方法存根时不会列出那些。 (请参阅前一个问题: 为什么VS Metadata视图不显示显式接口实现的成员 )
在发布此问题之前,我曾尝试检查List<T>
(通过Visual Studio中的“转到定义”)以查看是否需要实现多个版本的GetEnumerator。 我想这不是最可靠的检查方式。
无论如何,我将此标记为已回答。 谢谢你的帮助。
添加以下显式接口实现:
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
虽然IEnumerator<T>
是一个IEnumerator
,对于合同IEnumerable
返回IEnumerator
具体而言,不是一种IEnumerator<T>
实现IEnumerable<T>
,还必须显式实现IEnumerable.GetEnumerator()
。 泛型接口的方法本身作为非泛型方法契约的实现无效。 你可以有一个调用另一个,或者因为你有一个你正在使用的枚举器的子对象,只需复制/粘贴;
using System.Collections;
using System.Collections.Generic;
public class FooCollection : IEnumerable<Foo>
{
private Dictionary<string, Foo> fooDictionary = new Dictionary<string, Foo>();
public IEnumerator<Foo> 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<Foo> M()
是完全不同的方法。
解决方法是您必须显式实现非泛型版本:
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
// this calls the IEnumerator<Foo> GetEnumerator method
// as explicit method implementations aren't used for method resolution in C#
// polymorphism (IEnumerator<T> implements IEnumerator)
// ensures this is type-safe
return GetEnumerator();
}
只要通用IEnumerable {T}继承IEnumerable你就必须实现IEnumerable.GetEnumerator()
。 您可以明确地执行以下操作:
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
你已经有了几个主要问题的答案。 我会回答你编辑中提出的问题......
List<T>
类实际上有三种不同的GetEnumerator
方法:当编译时实例被输入为List<T>
本身时调用的公共方法,以及两个用于满足IEnumerable
/ IEnumerable<T>
契约的显式接口实现。 所有三种方法返回的枚举器对象都是幕后的List<T>.Enumerator
类型。
// Public method
public List<T>.Enumerator GetEnumerator() { /* ... */ }
// IEnumerable<T> explicit interface implementation
IEnumerator<T> IEnumerable<T>.GetEnumerator() { /* ... */ }
// IEnumerable explicit interface implementation
IEnumerator IEnumerable.GetEnumerator() { /* ... */ }
实现通用IEnumerable接口时,还必须实现非通用IEnumerable接口。 错误是关于缺少的非泛型方法。
这是IEnumerable的声明:
public interface IEnumerable<out T> : IEnumerable
{
new IEnumerator<T> GetEnumerator();
}
注意新的关键字。
这是IEnumerable的声明:
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
所以现在你有一个GetEnumerator方法,但是你实现了哪两个呢? 因此,您需要添加非泛型版本的显式实现:
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.