我有一个泛型类使用另一个类,它返回时需要知道什么样的初始类“拥有”它 - 这会导致问题;)让我举个例子:接口和泛型的双向引用
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(编译但在运行时失败)。我试图用各种方式调整代码,但我还没有找到一个可行的实现...
希望你看到我想要做的事情,也许你甚至看到我如何实现这一点; - )
这没有把戏!修复了一个问题并且学到了新的东西不错:)谢谢! –