本文介绍了IEnumerable.GetEnumerator()和IEnumerable< T> .GetEnumerator()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.net框架中有一个通用的 IEnumerable< T> 接口,该接口继承自非通用的 IEnumerable ,并且它们都有一个 GetEnumerator()方法.这两个 GetEnumerator()之间的唯一区别是返回类型.现在我有一个类似的设计,但是当我编译代码时,编译器说:

In the .net framework, there's a generic IEnumerable<T> interface which inherits from the not-generic IEnumerable, and they both have a GetEnumerator() method. The only differents between these two GetEnumerator() is the return type.Now I have a similar design, but when I compile the code, the compiler said:

MyInterface< T> .GetItem()'隐藏继承的成员' MyInterface.GetItem()'.如果打算隐藏,请使用新关键字.

MyInterface<T>.GetItem()' hides inherited member 'MyInterface.GetItem()'. Use the new keyword if hiding was intended.

MyInterface< T> .GetItem()返回具体的类型T,而 MyInterface.GetItem()返回类型System.Object.

The MyInterface<T>.GetItem() returns a concrete type T, while MyInterface.GetItem() returns type System.Object.

因此,我认为如果BCL团队成员编译.net框架,他们将收到相同的警告.

So I think if the BCL team guys compile the .net framework, they will get the same warning.

我认为编译器警告不好,您如何看待?我该如何解决这个问题呢?我的意思是我想在调用 MyInterface< T> .GetItem()时得到具体的类型T,而不仅仅是System.Object类型的实例.

I think having compiler warnings is not good, what do you think? And how can I solve this problem? I mean I want to get the concrete type T when calling the MyInterface<T>.GetItem() not just a instance of type System.Object.

提前谢谢!:-)

补充:我说的是接口本身:IMyInterface 继承自 IMyInterface,它们都具有GetItem()方法(IMyInterface.GetItem()返回类型T,而IMyInterface.GetItem()返回类型System.目的).问题是,如果我们的代码仅具有这两个接口,即没有派生的具体类,则在编译源代码后,我们将遇到编译器警告.

Supplement:I'm saying the interfaces theirselves: IMyInterface inherits from IMyInterface, and they both have the GetItem() method (the IMyInterface.GetItem() returns type T, while IMyInterface.GetItem() returns type System.Object). The problem is that, if our code only have these two interfaces, that is, no derived concrete classes, we will encounter the compiler warning after compile the source code.

推荐答案

之所以没有这样做,是因为它们将一个版本编译为显式接口方法实现.看起来像这样:

They don't because they compile one version as an Explicit Interface Method Implementation. It looks like this:

public class SomeClassThatIsIEnumerable<T> : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator()
    {
       // return enumerator.
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable<T>)this).GetEnumerator();
    }
}

这种构造类型的作用是使第一个GetEnumerator方法成为默认方法,而另一个GetEnumerator方法仅在调用者首先将SomeClassThatIsIEnumerable转换为IEnumerator类型时才可访问.

What this type of construct does is make the first GetEnumerator method become the default method, while the other GetEnumerator method is only accessible if the caller first casts SomeClassThatIsIEnumerable to the type IEnumerator, so it avoids the problem.

根据上面的补充,您想使用新关键字:

based on the supplement above, you would want to use the new keyword:

public interface IMyInterface
{
   object GetObject();
}

public interface IMyInterface<T> : IMyInterface
{
   new T GetObject();
}

// implementation:

public class MyClass : IMyInterface<string>
{
    public string GetObject()
    {
        return "howdy!";
    }

    object IMyInterface.GetObject()
    {
        return GetObject();
    }
}

这篇关于IEnumerable.GetEnumerator()和IEnumerable&lt; T&gt; .GetEnumerator()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:35