简体   繁体   English

与接口的协方差-接口的枚举

[英]Covariance with Interface - enumeration of interface

I have a class like this: 我有这样的课:

public partial class GmxGlobalList : DynamicBindingListBase<GmxGlobal> 

Where DynamicBindingListBase implements Collection<T> which extends from IEnumerable<T> - in this case IEnumerable<GmxGlobal> . 其中DynamicBindingListBase实现从IEnumerable<T>扩展的Collection<T> ,在本例中为IEnumerable<GmxGlobal>

The class GmxGlobal is defines like GmxGlobal类的定义类似于

public partial class GmxGlobal : IGmxGlobal 

I want now to declare GmxGlobalList like 我现在GmxGlobalList这样声明GmxGlobalList

public partial class GmxGlobalList : DynamicBindingListBase<GmxGlobal>, IEnumerable<IGmxGlobal>

But this does not work, because I should declare the Method IEnumerator<IGmxGlobal> GetEnumerator() - but this is already implemented from Collection<T> - but not with IGmxGlobal but with GmxGlobal . 但这是行不通的,因为我应该声明方法IEnumerator<IGmxGlobal> GetEnumerator() -但这已从Collection<T> -但不是使用IGmxGlobal而是使用GmxGlobal I would ask you, why I have to implement IEnumerator<IGmxGlobal> GetEnumerator() ? 我会问你,为什么我必须实现IEnumerator<IGmxGlobal> GetEnumerator() - What sould I do, that I can declare, that GmxGlobalList is an IEnumerable or ICollection or IList of IGmxGlobal (or something other where IGmxGlobal can be enumerated). -什么高度重视和我做,我可以宣布,该GmxGlobalList是IEnumerableICollectionIListIGmxGlobal (或其他一些地方IGmxGlobal可以列举)。

Since DynamicBindingListBase<T> implements IEnumerable<T> and GmxGlobalList inherits from DynamicBindingListBase<GmxGlobal> and implements IEnumerable<IGmxGlobal> . 由于DynamicBindingListBase<T>实现IEnumerable<T> ,因此GmxGlobalList继承自DynamicBindingListBase<GmxGlobal>并实现IEnumerable<IGmxGlobal>
GmxGlobalList implements both IEnumerable<GmxGlobal> and IEnumerable<IGmxGlobal> . GmxGlobalList实现IEnumerable<GmxGlobal>IEnumerable<IGmxGlobal>

I'm going to assume that DynamicBindingListBase<T> already has a concrete implementation of IEnumerable<T> . 我将假设DynamicBindingListBase<T>已经具有IEnumerable<T>的具体实现。 This implementation wouldn't work as a IEnumerable<IGmxGlobal> since IEnumerable<IGmxGlobal> has a wider scope than IEnumerable<GmxGlobal> . 此实现不能用作IEnumerable<IGmxGlobal>因为IEnumerable<IGmxGlobal>的范围比IEnumerable<GmxGlobal>更大。

If you were to just have the following: 如果您仅需具备以下条件:

GmxGlobalList : DynamicBindingListBase<GmxGlobal>

Due to IEnumerable being co-variant, you can already use GmxGlobalList as a IEnumerable<IGmxGlobal> . 由于IEnumerable是协变的,因此您已经可以将GmxGlobalList用作IEnumerable<IGmxGlobal> Like this: 像这样:

IEnumerable<IGmxGlobal> foo = new GmxGlobalList();

If you really need to implement IEnumerable<IGmxGlobal> you can write the implementation details to wrap around DynamicBindingListBase<GmxGlobal> . 如果确实需要实现IEnumerable<IGmxGlobal> ,则可以编写实现细节以包装DynamicBindingListBase<GmxGlobal>

public IEnumerator<IGmxGlobal> GetEnumerator()
{
    return (base as IEnumerator<GmxGlobal>).GetEnumerator();
}

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

这样行吗?

IEnumerable<IGmxGlobal> e = (IEnumerable<GmxGlobal>)instanceOfGmxGlobalList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM