“Autofac 会自动选择能够从容器中获取的参数最多的构造函数。” 我希望它不这样做,而是选择默认构造函数。http://code.google.com/p/autofac/wiki/Autowiring
internal class ParameterlessConstructorSelector : IConstructorSelector
{
#region Implementation of IConstructorSelector
/// <summary>
/// Selects the best constructor from the available constructors.
/// </summary>
/// <param name="constructorBindings">Available constructors.</param>
/// <returns>
/// The best constructor.
/// </returns>
public ConstructorParameterBinding SelectConstructorBinding(ConstructorParameterBinding[] constructorBindings)
{
return constructorBindings.First();
}
#endregion
}
当我连接课程时,我这样做了:
builder.RegisterType<EmployeeFactory>()
.As<IEmployeeFactory>().UsingConstructor(new ParameterlessConstructorSelector())
.SingleInstance();
constructorBindings 列表中的第一个绑定始终是具有无参数构造函数的绑定。不确定它是先定义还是 autofac 扫描构造函数的方式,但这是为无参数构造函数连接的正确方法吗?
谢谢