Can NInject load modules/assemblies on demand?

2019-02-05 08:56发布

Are there facilities in NInject that will allow me to load services from other modules (assemblies) on demand like it's done in Unity?

3条回答
你好瞎i
2楼-- · 2019-02-05 09:21

I'm pretty sure this is what you're looking for:

var kernel = new StandardKernel();
kernel.Load( Assembly.Load("yourpath_to_assembly.dll");

If you look at KernelBase with reflector in Ninject.dll you will see that this call will recursively load all modules in the loaded assemblies (Load method takes an IEnumerable)

public void Load(IEnumerable<Assembly> assemblies)
{
    foreach (Assembly assembly in assemblies)
    {
        this.Load(assembly.GetNinjectModules());
    }
}
查看更多
仙女界的扛把子
3楼-- · 2019-02-05 09:26

If you're referring to loading Assemblies non-statically out of the box, no it doesnt.

There are many other questions on this, e.g., Using Ninject in a plugin like architecture

查看更多
Lonely孤独者°
4楼-- · 2019-02-05 09:28

I don't quite understand what you mean by "Like Unity" but you can do a few different things for loading assemblies. Ninject itself will load local assemblies for extensions/plugins by default. Ninject can also load NinjectModule classes from assemblies. If you want to do something more complex, you can use the Ninject.Extensions.Conventions project to do a lot of different scanning and type binding.

查看更多
登录 后发表回答