0

I have already looked at some similar answers but I cannot get it to work.

I am attempting to make the following more maintainable:

var modules = new INinjectModule[]
{
    new ServiceModule(),
    new ApplicationSettingsModule(),
    new SerializerModule(),
    new LoggerModule(),
    new SqliteModule(),
    new SetupModule(), 
    new CacheModule(), 
    new AuthenticationModule(), 
};

Every time I add a new NinjectModule I need to modify this array to include it.

I want to be able to find all types that derive from NinjectModule and activate them and put them all into a collection.

This is what I have tried but I am not getting any of my classes that derive from NinjectModule

var classes = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
                from assemblyType in domainAssembly.GetTypes()
                where typeof(NinjectModule).IsAssignableFrom(assemblyType)
                select assemblyType).ToArray();

Please note that the classes that I want to find are in a different assembly...

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • `AppDomain.CurrentDomain.GetAssemblies()` returns only assemblies that have been loaded at the time you call it (see http://stackoverflow.com/a/10284950/292411) - you could check whether the assemblies that contain the desired types are included at that point – C.Evenhuis Dec 24 '15 at 13:28

2 Answers2

1

I suggest you to use kernel like that, so the Ninject will take care about the NinjectModules:

public static IKernel ConfigureKernel(IKernel kernel)
{
      kernel.Load(Assembly.Load("NZBDash.DependencyResolver.Modules"));
      return kernel;
}
Joel R Michaliszen
  • 4,164
  • 1
  • 21
  • 28
0

Fixed it.

I used Assembly.Load():

var result = Assembly.Load("NZBDash.DependencyResolver.Modules").GetTypes()
               .Where(a => 
               a.IsClass &&
               a.BaseType == typeof(NinjectModule))
               .ToArray();
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83