Inject Array of Interfaces in Ninject

前端 未结 4 1572
梦如初夏
梦如初夏 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:41

    Since Array implements IReadOnlyList the following works.

       // Binding
       public sealed class FooModule: NinjectModule 
       {
         public opverride void Load() 
         {
            Bind>().ToConstant(new IFoo[0]);
          }
       }
    
       // Injection target
       public class InjectedClass {
          public InjectedClass(IReadOnlyList foos) { ;}
       }
    

提交回复
热议问题