2011-03-17 76 views

回答

10

您可以嘗試Ninject.Extensions.Conventions

var kernel = new StandardKernel(); 
kernel.Bind(c => 
      c.FromThisAssembly() 
       .SelectAllClasses().InheritedFrom<IFoo>() 
       .BindAllInterfaces()); 

// and later: 

kernel.GetAll<IFoo>().ToList().ForEach(foo => foo.DoSmth()); 

所需的類如下:

public interface IFoo 
{ 
    void DoSmth(); 
} 

public class Foo1 : IFoo 
{ 
    public void DoSmth() 
    { 
     Console.Out.WriteLine("Foo1"); 
    } 
} 

public class Foo2 : IFoo 
{ 
    public void DoSmth() 
    { 
     Console.Out.WriteLine("Foo2"); 
    } 
}