如何在实现IEnumerable&LT我的字典包装类实现IEnumerable;美孚&GT ;? [英] How do I implement IEnumerable in my Dictionary wrapper class that implements IEnumerable<Foo>?

查看:146
本文介绍了如何在实现IEnumerable&LT我的字典包装类实现IEnumerable;美孚&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个词典℃的包装;弦乐,富方式>



字典<弦乐,富> 工具的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> implements IEnumerable<KeyValuePair<String,Foo>>, but I want my wrapper class to implement IEnumerable<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 an IEnumerator<Foo>, and IEnumerator<Foo> is an IEnumerator.

EDIT:

The solution of explicitly implementing IEnumerator.GetEnumerator() works. However I'm now wondering why when I "Go to definition" on a List<T> I see only one definition of GetEnumerator: public List<T>.Enumerator GetEnumerator();

Apparently List<T> can have a single GetEnumerator method that returns something that implements both IEnumerator<T> and IEnumerator, 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 an IEnumerator, the contract for IEnumerable returns an IEnumerator specifically, not an IEnumerator<T>

这篇关于如何在实现IEnumerable&LT我的字典包装类实现IEnumerable;美孚&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆