Inject Array of Interfaces in Ninject

前端 未结 4 1564
梦如初夏
梦如初夏 2020-12-15 22:59

Consider the following code.

public interface IFoo { }


public class Bar
{
    public Bar(IFoo[] foos) { }
}


public class MyModule : NinjectModule
{
    p         


        
4条回答
  •  春和景丽
    2020-12-15 23:44

    Ninject supports multi injection which would resolve your issue. https://github.com/ninject/ninject/wiki/Multi-injection

    public interface IFoo { }
    public class FooA : IFoo {}
    public class FooB : IFoo {}
    
    public class Bar
    {
        //array injected will contain [ FooA, FooB ] 
        public Bar(IFoo[] foos) { }
    }
    
    public class MyModule : NinjectModule
    {
        public override void Load()
        {
            Bind().To();
            Bind().To();
            //etc..
        }
    }
    

提交回复
热议问题