开发者

Calling all ISomething instances in Ninject

I have an interface ISomething with a method Start. I want to get all implementations of this interface (in multiple assemblies, the main one and all referenced ones) and call the Start method on application start. How can I do this with Ninject 2.2.开发者_JAVA百科0.0 .NET 4.0?

Autofac answer was here Calling all ISomething instances in Autofac


You may try 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());

Needed classes are below:

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");
    }
}


You could use reflection to find all classes that implement the interface(s): http://cocaine.co.nz/Home/High-On-Ninject-BLLModule

What do you mean by the "main one"? - call the Start() method on which one?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜