StructureMap: Programmatically retrieve list of instance keys from StructureMap.config

StackOverflow https://stackoverflow.com/questions/1622832

  •  06-07-2019
  •  | 
  •  

Question

I've defined a for a specific interface within my StructureMap.config. There can be many different available concrete types defined.

I would like to programmatically retrieve a list of available instance keys (names) that are currently available without actually parsing the StructureMap.config file itself. Is there any way to do this?

Was it helpful?

Solution

Yes, you can inspect the container using its Model property.

For example, the following code will print the names of all instances of IWidget:

foreach(var instance in container.Model.InstancesOf<IWidget>())
{
    Console.WriteLine(instance.Name);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top