【发布时间】:2011-11-02 15:14:00
【问题描述】:
我有一个带有泛型的类,它使用另一个类,作为回报,它需要知道初始类的哪个实例“拥有”它 - 这会导致问题;)让我举个例子:
public interface IFoo<T>
{
}
public interface IBar
{
IFoo<IBar> Foo { get; set; }
}
public class Foo<T> : IFoo<T> where T : IBar, new()
{
private readonly T _bar;
public Foo()
{
_bar = new T {Foo = this};
}
}
class Bar : IBar
{
public IFoo<IBar> Foo { get; set; }
}
这不起作用,因为 Foo = 这不起作用 - 即使我尝试将其强制转换为 IFoo(编译但在运行时失败)。我尝试以各种方式调整代码,但我没有找到可行的实现......
希望您能看到我正在尝试做的事情,也许您甚至会看到我如何实现这一目标 ;-)
【问题讨论】:
标签: c# generics inheritance c#-4.0 interface