如何在实现IEnumerable<我的字典包装类实现IEnumerable;美孚> ;? [英] How do I implement IEnumerable in my Dictionary wrapper class that implements IEnumerable<Foo>?
问题描述
我试图创建一个词典℃的包装;弦乐,富方式>
字典<弦乐,富>
工具的IEnumerable< KeyValuePair<弦乐,富>>
,但我想我的包装类来实现的IEnumerable<富>
。所以,我想这样的:
公共类FooCollection:IEnumerable的<富>
{
私人字典<字符串,富> fooDictionary =新词典<字符串,富>();
公众的IEnumerator<富>的GetEnumerator()
{
返回fooDictionary.Values.GetEnumerator();
}
//其他包装方法省略
}
不过,我得到这个错误:
'FooCollection不实现接口成员'System.Collections.IEnumerable.GetEnumerator()。 FooCollection.GetEnumerator()无法实现System.Collections.IEnumerable.GetEnumerator()',因为它不具备System.Collections.IEnumerator'匹配的返回类型。块引用>
不过,我不明白这个错误,因为
FooCollection.GetEnumerator()
返回的IEnumerator<富>
和的IEnumerator<富方式>
是的IEnumerator
编辑:
的实施明确
IEnumerator.GetEnumerator()解决方案
的作品。但是我现在不知道为什么,当我在列表<去定义; T>
我看到的GetEnumerator只有一个定义:
公开名单< T> .Enumerator的GetEnumerator();
显然
列表< T>
可以返回的东西,同时实现了的IEnumerator℃的单一
和的GetEnumerator
方法; T>IEnumerator的
,但我必须为每一个方法
编辑:
作为回答下面LukeH,
列表< T>
的确实的包括:显式接口实现。显然的Visual Studio刚刚从元数据生成方法存根时,不会列出这些。 (见本前面的问题:为什么VS元数据视图不显示显式接口实现成员的)
在发布这个问题我曾试图检查
列表< T> ;
(在Visual Studio中通过转到定义),看看是否我需要实现的GetEnumerator的多个版本。我想这是不检查最可靠的方式。
无论如何,我打这个作为回答。感谢您的帮助。
解决方案添加以下显式接口实现:
的IEnumerator IEnumerable.GetEnumerator()
{
返回this.GetEnumerator();
}
虽然
的IEnumerator< T>
是的IEnumerator
为的IEnumerable
合同返回的IEnumerator
具体而言,不是一个的IEnumerator< T>
I'm trying to create a wrapper for a
Dictionary<String,Foo>
.
Dictionary<String,Foo>
implementsIEnumerable<KeyValuePair<String,Foo>>
, but I want my wrapper class to implementIEnumerable<Foo>
. So I tried this: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 }
However I get this error:
'FooCollection' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'FooCollection.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'.However I don't understand this error, because
FooCollection.GetEnumerator()
returns anIEnumerator<Foo>
, andIEnumerator<Foo>
is anIEnumerator
.EDIT:
The solution of explicitly implementing
IEnumerator.GetEnumerator()
works. However I'm now wondering why when I "Go to definition" on aList<T>
I see only one definition of GetEnumerator:public List<T>.Enumerator GetEnumerator();
Apparently
List<T>
can have a singleGetEnumerator
method that returns something that implements bothIEnumerator<T>
andIEnumerator
, but I have to have one method for each?EDIT:
As answered by LukeH below,
List<T>
does include the explicit interface implementations. Apparently Visual Studio just doesn't list those when generating method stubs from the metadata. (See this previous question: Why does the VS Metadata view does not display explicit interface implemented members )Before posting this question I had tried checking
List<T>
(via "Go to Definition" in Visual Studio) to see if I needed to implement multiple versions of GetEnumerator. I guess this wasn't the most reliable way to check.Anyway, I'm marking this as answered. Thanks for your help.
解决方案Add the following explicit interface implementation:
IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); }
Although
IEnumerator<T>
is anIEnumerator
, the contract forIEnumerable
returns anIEnumerator
specifically, not anIEnumerator<T>
这篇关于如何在实现IEnumerable<我的字典包装类实现IEnumerable;美孚> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!