我想注册具有多个构造函数的程序集类型 Autowiring 选择了错误的构造函数并想像我在 RegisterType 中那样指定它
builder.RegisterType(typeof(IController))
.UsingConstructor(typeof(IUnitOfWork));
但是当我这样做的时候
builder.RegisterAssemblyTypes(typeof(IController).Assembly)
.UsingConstructor(typeof(IUnitOfWork));
我明白了
“类型‘System.Object’上不存在匹配的构造函数。”
我认为这是因为程序集类型比我想象的要复杂一些,但问题仍然悬而未决
我该怎么办?
最佳答案
通过做
builder.RegisterType(typeof(IController))
.UsingConstructor(typeof(IUnitOfWork));
您在 Autofac 容器中注册 IController 并告诉它应该使用带有 IUnitOfWork
参数的构造函数。
UsingConstructor
方法不适用于 RegisterAssemblyTypes
但在您的情况下,您可以使用 FindConstructorWith
方法。
builder.RegisterAssemblyTypes(typeof(IController).Assembly)
.FindConstructorsWith(t => new[] {
t.GetConstructor(new[] { typeof(IUnitOfWork) })
})
.As<IController>();
关于c# - 使用 UsingConstructor 注册 AssemblyTypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12782680/