How do I get the module reference instead of ModuleInfo?

Topics: Prism v2 - WPF 4
Aug 3, 2010 at 8:45 PM
In my shell's view model I have a list of objects that will represent a list of menu options. I populate this list when the ViewModel is created by looping through the modules in the module catalog : foreach (var itm in _moduleCatalog.Modules) The problem that I'm having is that I need to reference the actual Module. I need it because my Module contains a property that points to it's main View object and I need access to that View. When I loop through the Modules list I only receive the ModuleInfo class type instead of the actual one I need. How can I do this?
Aug 4, 2010 at 7:18 AM

Create an helper function like that
private T TryResolve<T>(String name)
		{
			try
			{
				return _container.Resolve<T>(name);
			}
			catch
			{
				return default(T);
			}
		}
And resolve your module instance like
IModule module = TryResolve<IModule>(e.ModuleInfo.ModuleName)
Aug 4, 2010 at 2:59 PM
That sounds like it might work but this line: "IModule module = TryResolve<IModule>(e.ModuleInfo.ModuleName)" raises an exception that says IModule is an interface and cannot be constructed.
Aug 4, 2010 at 3:40 PM
Edited Aug 4, 2010 at 3:45 PM
programmeranalyst wrote:
That sounds like it might work but this line: "IModule module = TryResolve<IModule>(e.ModuleInfo.ModuleName)" raises an exception that says IModule is an interface and cannot be constructed.

I found a work around thanks to slouge's help. First I get module info so I can get a reference to the type name. Then, I set that to a type and pass it into the resolve method: [code] object module; ModuleInfo modInfo = _moduleCatalog.Modules.FirstOrDefault(f => f.ModuleName == moduleName); if (modInfo != null) { Type t = Type.GetType(modInfo.ModuleType); module = _container.Resolve(t); } [/code]
Aug 30, 2010 at 6:09 PM

I tried programmeranalyst's code, but it is creating a new instance of the module. I want to get the instance of the module specified by the ModuleInfo; I don't want to create a new instance. Anyone know how to do this? I don't see why it's so difficult to get a list of the loaded IModules. Maybe that's something we're not supposed to be doing?

Sep 11, 2010 at 11:59 AM

Put in Intialize method

public void Initialize()
  {
   _container.RegisterInstance<IModule>("NameOfYourModule", this);
  }

 

and use the method showed in my first post