【发布时间】:2018-07-20 06:41:13
【问题描述】:
void Start () {
IKernel nKernel = new StandardKernel();
nKernel.Bind<IAnimal>().To(typeof(Dog)).Named("Dog");
nKernel.Bind<IAnimal>().To(typeof(Cat)).Named("Cat");
IAnimal animalInst = nKernel.Get<IAnimal>("Dog");
Debug.LogError($"{animalInst.Name}");
IAnimal animalInst2 = nKernel.Get<IAnimal>("Cat");
Debug.LogError($"{animalInst.Name}");
}
我想在运行时动态实例化 IAnimal 的子类,我试图运行这段代码,但它发生错误 "IAnimal animalInst2 = nKernel.Get("Cat");" => ActivationException:错误激活浮动 没有匹配的绑定可用,并且该类型不可自绑定。 如何在一个接口上绑定子类?我可以通过类类型或类名来获取它吗?
【问题讨论】:
标签: c# dependency-injection inversion-of-control ninject bind