问题描述
在使用嵌套泛型集合实现设计时,我偶然发现了这些限制显然是由C#的不变的泛型造成的:
While implementing a design using nested generic collections, I stumbled across those limitations apparently caused by C#'s invariant Generics:
Cannot convert from 'Collection<subtype of T> to 'Collection<T>'
这意味着,以下操作将不起作用,显然是由于泛型的不变性:
That means, the following will not work, apparently due to the invariance of Generics:
class Outer<TInner, TInnerItem> where TInner : Inner<TInnerItem>
{
public void Add(TInner item)
{
item.Outer = this; // ERROR:
// Cannot implicitly convert from Outer<TInner, TInnerItem>
// to Outer<Inner<TInnerItem>, TInnerItem>
}
}
class Inner<TInnerItem> : ICollection<TInnerItem>
{
Outer<Inner<TInnerItem>, TInnerItem> _outer;
public Outer<Inner<TInnerItem>, TInnerItem> Outer
{
set { _outer = value; }
}
}
(在实际代码中, Inner<>
和外部<>
实现 ICollection< 。)
(In the actual code, both Inner<>
and Outer<>
implement ICollection<>
.)
我需要 Inner<>
对象来引用其容器集合,以便访问一些数据。
I need the Inner<>
objects to have a reference to its container collection in order to access some of its data.
如何实现这些嵌套集合,最好使用上面概述的通用方法?如何在 Inner<>
类中设置对容器集合的引用?
How would you implement these nested collections, preferably using a generic approach as outlined above? How would you set the reference to the container collection in the Inner<>
class?
介绍一个(可能是抽象的)基类。
Cheers!
推荐答案
不依赖于TInner可以帮助你:
Introducing a (possibly abstract) base class which is not dependant on TInner may help you:
abstract class OuterBase<TInnerItem>
{
}
class Outer<TInner, TInnerItem> : OuterBase<TInnerItem> where TInner : Inner<TInnerItem>
{
public void Add(TInner item)
{
item.Outer = this; // Compiles
}
}
class Inner<TInnerItem> : ICollection<TInnerItem>
{
OuterBase<TInnerItem> _outer;
public OuterBase<TInnerItem> Outer
{
set { _outer = value; }
}
}
或者等待C#4.0,反变异通用接口。
Or wait for C# 4.0, which introduces co/contra-variant generic interfaces.
这篇关于嵌套的通用集合:如何实现从项到容器的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!