Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructureMap get registered types, not instances

I have a plugin system that allows a user to choose the type of plugin they wish to create (basically this sets up a configuration for a plugin instance).

They choose the plugin type from a select list. I am using StructureMap to inject an IEnumerable into my MVC controller so that I can then access the FQ type name to use in the select list.

This works fine but I don't really like that I having to create an instance of all the registered plugins just to display a list of them in a select list.

So the question is, can I access the types of IPlugin that are registered with StructureMap?

like image 387
Ben Foster Avatar asked Apr 13 '11 10:04

Ben Foster


1 Answers

You should be able to get the instance information by using the Model property of the container:

IContainer container = ObjectFactory.Container;
IEnumerable<InstanceRef> instances = container.Model.AllInstances.
  Where(i => i.PluginType.Equals(typeof(IPlugin)));

You can access the Concrete type using:

foreach(var instanceRef in instances)
  Console.WriteLine(instanceRef.ConcreteType);
like image 176
PHeiberg Avatar answered Oct 31 '22 00:10

PHeiberg