我有一个接口(interface)IAdapdor
,以及几个具体的实现。使用 Ninject 3.0,我按名称绑定(bind)它们:
IKernel kernel = new StandardKernel();
kernel.Bind<IAdapdor>().To<Adaptor1>().Named("Adaptor1");
kernel.Bind<IAdapdor>().To<Adaptor2>().Named("Adaptor2");
...
如何使用 Ninject conventions extension 实现此目的? ?
更具体地说,我正在查找行中的内容:
kernel.Bind(x => x.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom<IAdapdor>()
.BindByClassName()); // <-- BindByClassName() does not really exist
最佳答案
您可以使用 Configure
method 自定义约定创建的绑定(bind).所以你可以使用它来注册你的 Named
绑定(bind):
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses().InheritedFrom<IAdapdor>()
.BindAllInterfaces()
.Configure((b, c) => b.Named(c.Name)));
关于c# - 在 Ninject 中按名称绑定(bind)所有具体实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21376924/