Ninject是否同时支持多次注射和工厂方法?

时间:2015-10-23 09:57:10

标签: c# dependency-injection ninject

Ninject支持多次注射:

public class Samurai 
{
    public Samurai(IWeapon[] allWeapons) 
    {
        var allWeapons = allWeapons;
    }
}

它还支持自动生成的工厂:

public class Samurai 
{
    public Samurai(Func<IWeapon> weaponFactory) 
    {
        var weapon = weaponFactory.Invoke();
    }
}

不知何故,它不能同时处理这两个功能:

public class Samurai 
{
    public Samurai(Func<IWeapon>[] weaponFactories) 
    {
        var weapons = weaponFactories.Select(factory => factory.Invoke());
    }
}

结果:

Error activating IntPtr
No matching bindings are available, and the type is not self-bindable.

有没有办法实现这一点,还是我必须手动实施工厂?

1 个答案:

答案 0 :(得分:0)

将构造函数更改为:

public Samurai(IEnumerable<IWeapon> weaponFactories) 
每次迭代集合时,

IEnumerables都会产生一个新实例(如果生活方式是短暂的)。所以在某种意义上,IEnumerable是工厂(它表现为流)。