In earlier versions of Ninject.Extensions.Conventions, it was pretty easy to scan a directory for assemblies, filter classes by interface and then load all containing ninject modules.
kernel.Scan(scanner =>
scanner.FromAssembliesInPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
scanner.AutoLoadModules();
scanner.WhereTypeInheritsFrom<IPlugin>());
public class MyPlugin : NinjectModule, IPlugin {
public override void Load() {
Bind<IRepositoryFromPluginHost>().To<MyPluginImpl>().Named("special");
}
}
However, after I updated lately to the newest release, everything seems gone and I'm unable to
Does anybody have a solution for this?
There's still the https://github.com/ninject/ninject.extensions.conventions extension. However, the interface has changed, to something along the lines of:
kernel.Bind(x =>
{
x.FromAssembliesInPath("somepath")
.IncludingNonePublicTypes()
.SelectAllClasses()
.InheritedFrom<IPlugin>()
.BindDefaultInterface() // Binds the default interface to them;
});
Update:
How about you bind all IPlugin
to IPlugin
using the conventions extension (as above), and then do:
var plugins = IResolutionRoot.GetAll<IPlugin>();
kernel.Load(plugins);
Maybe the hard way, but something like this will get you a list of types that are derived from NinjectModule.
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
List<Type> types = new List<Type>();
foreach (var assembly in assemblies)
{
types.AddRange(GetModules(assembly));
}
IEnumerable<Type> GetModules(Assembly assembly)
{
assembly.GetTypes()
.Where(t => t.BaseType == typeof(NinjectModule));
}
To load your module try this.
(Activator.CreateInstance(type) as NinjectModule).Load();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With