0
我有相互双向连接的类。 当Ninject创建类Parent
时,它也会创建一个Child
。问题是Child
必须知道它的父母是谁。但我在IContext
中找不到关于父对象的任何信息。获取Ninject中的父对象
//The parent
class Parent:IParent
{
public Parent(Child child) {...}
}
//The child needing to know who its parent is
class Child:IChild
{
public Child(Parent parent) {...}
}
//The Ninject binding
Bind<IChild>().To<Child>.WithConstructorArgument("parent", x=>GetParent(x));
Bind<IParent>().To<Parent>;
Bind<IFactory>().ToFactory();
//Method to get the constructor parameter to Child containing the parent
private IParent GetParent(IContext context)
{
// Should return the IParent that requested this IChild
}
当我打电话给IFactory.CreateParent()
我想获得一个双向连接到一个孩子的父。
请问你能详细解释一下你想完成什么吗?当你调用'Resolve'时,你不会将有关调用者的任何信息传递给内核。 –
我看到我为Bind写了一个错误。它现在被纠正 – magol
当我打电话给IFactory.CreateParent()我想要一个双向连接到一个孩子的父。 – magol