Enumerate all loaded Modules

Jul 2, 2008 at 8:33 PM
Hi there!

Is it possible to enumerate all loaded modules?
I want to get all IModule Instances that are currently loaded for further processing.

Best regards
Alexander
Jul 3, 2008 at 8:14 AM
Hi there lippling, it's pretty easy to do this if you're using the default UnityBootstrapper. Simply resolve IModuleEnumerator from the container, I seem to say this a lot in my replies but check out my samples project it demonstrates this in the LateNight projects About screen.

Click here for the samples project page, be sure to grab the project from source as it's got a lot more in it than the last build I made. Snippet from samples project is provided below.

     public LateNightAboutModel(IUnityContainer container) {
         IModuleEnumerator moduleEnum = container.Resolve<IModuleEnumerator>();
         moduleInfos = moduleEnum.GetModules();
     }

I then create a DataTemplate in the view to bind to the infos in a ListView.

I hope this helps you out.
-Brett
Jul 3, 2008 at 11:13 AM
Hi Brett,

yes, that would work. But your approach only provides all available modules. My requirement is to get all loaded and initialized modules.

Best regards
Alexander
Jul 3, 2008 at 1:34 PM
Edited Jul 3, 2008 at 1:35 PM


lippling wrote:
Hi Brett,

yes, that would work. But your approach only provides all available modules. My requirement is to get all loaded and initialized modules.

Best regards
Alexander



somewhere where you have the container accessable.

 

IEnumerable<IModule> modules = this.Container.ResolveAll<IModule>();   maybe?

And if you have nested containers, you may/will have to loop over them too.

 

 

 

Jul 3, 2008 at 3:13 PM
When I do a
var m = new List<IModule>(Container.ResolveAll<IModule>());
m.Count is 0

I think the modules are registered with their concrete type and not with IModule.
Jul 3, 2008 at 4:40 PM
The list of initialized modules is there inside the ModuleLoader class. (look for initializedModules member). The problem is that it is a private member. So I think we have 2 options
- Use reflection
- Change the class/interface to expose a property
Jul 4, 2008 at 12:39 PM


lippling wrote:
When I do a
var m = new List<IModule>(Container.ResolveAll<IModule>());
m.Count is 0

I think the modules are registered with their concrete type and not with IModule.


Sorry,  I dont have access to the code right now, but I looked and thought that they were doing the registation using IModule.

If not, then you are right.  Oh well, thats why  I put the "Maybe?". :)

You would also be able "I think" to get a list of all of the registered classes in the container and then enumerate them looking for IModules.  Brut force, but should get them all.  But you would still have to deal with any chilid containers.

Paul