当然 - 您可以使用您的自定义实例提供程序。
[Fact]
public void CustomInstanceProviderTest()
{
const string Name = "theName";
const int Length = 1;
const int Width = 2;
this.kernel.Bind<ICustomizableWeapon>().To<CustomizableSword>().Named("sword");
this.kernel.Bind<ICustomizableWeapon>().To<CustomizableDagger>().Named("dagger");
this.kernel.Bind<ISpecialWeaponFactory>().ToFactory(() => new UseFirstParameterAsNameInstanceProvider());
var factory = this.kernel.Get<ISpecialWeaponFactory>();
var instance = factory.CreateWeapon("sword", Length, Name, Width);
instance.Should().BeOfType<CustomizableSword>();
instance.Name.Should().Be(Name);
instance.Length.Should().Be(Length);
instance.Width.Should().Be(Width);
}
private class UseFirstParameterAsNameInstanceProvider : StandardInstanceProvider
{
protected override string GetName(System.Reflection.MethodInfo methodInfo, object[] arguments)
{
return (string)arguments[0];
}
protected override Parameters.ConstructorArgument[] GetConstructorArguments(System.Reflection.MethodInfo methodInfo, object[] arguments)
{
return base.GetConstructorArguments(methodInfo, arguments).Skip(1).ToArray();
}
}