NInject 相当于 Autofac 的 AsClosedTypesOf
以下使用 Autofac 的代码的 NInject 等效项是什么:
var builder = new ContainerBuilder();
System.Reflection.Assembly assembly = ...;
builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(OpenGeneric<>))
.As<IAnInterface>();
var resolved = container.Resolve<IEnumerable<IAnInterface>>();
What is the NInject equivalent of the following code that uses Autofac:
var builder = new ContainerBuilder();
System.Reflection.Assembly assembly = ...;
builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(OpenGeneric<>))
.As<IAnInterface>();
var resolved = container.Resolve<IEnumerable<IAnInterface>>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Ninject 3.0.0-rc3,您可以使用
根据您的要求,您可以删除
WhichAreGeneric
语句。.SelectAllClasses().InheritedFrom(typeof(BaseService<>)).WhichAreGeneric()
选择要创建绑定的类。约定确保接口和实现类必须具有相同的开放类型参数。例如,如果
IBar
是Bar
的唯一有效接口。但对于 Foo 来说,IBar, IFoo
都是有效的。Using Ninject 3.0.0-rc3 you can use
Depending on your requirements you can probably remove the
WhichAreGeneric
statement..SelectAllClasses().InheritedFrom(typeof(BaseService<>)).WhichAreGeneric()
selects the classes to which a binding is created.Conventions ensures that the interface and the implementation class must have the same open type arguments. E.g. In case
IBar<T1, T2>
is the only valid interface forBar<T1, T2>
. But for Foo bothIBar<int, int>, IFoo
are valid.