「Autofac自動選擇能夠從容器中獲取的參數最多的構造函數。」我希望它做其他事情,而不是選擇默認的構造函數。 http://code.google.com/p/autofac/wiki/AutowiringAutofac無參數構造函數選擇
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列表綁定始終是一個與paremeterless構造。不知道它是第一次定義還是autofac掃描構造函數的方式,但是這是用於無參數構造函數的正確方法嗎?
感謝
我知道我很幸運First():)感謝您的詳細解釋。 –
現在Autofac沒有'InvalidOperationException',而是有一個'DependencyResolutionException'類,如果沒有找到無參數的ctor就更合適。 – aholmes