Get instance from StructureMap by name type

Is there a way to request an instance from StructureMap ObjectFactory by type string name? For example, it would be nice to do something like this:

var thing = ObjectFactory.GetInstance("Thing");

This uses a messaging script in which the message is very general and contains only the task name. The handler receives the message, gets the task name from the message, and retrieves the type name of the associated task runner from the configuration database. StructureMap scans all the assemblies in the directory, and one of them (presumably) will contain the type returned from the config database, which must then be created.

Another option is to capture an instance of Type by doing the following:

var type = Type.GetType("Thing");

But the problem is that the assembly may or may not / may not be loaded into the AppDomain, so a reflection call is not always possible.

+3
source share
1 answer

I recently had the same issue GetTypethat did not return a type event, although StructureMap loaded it correctly from the scanned assembly.

My problem was that I did not use the name for the assembly, and I assume that without this, the method GetTypejust looks in the current assembly.

In any case, adding the full name sorted my problem.

Hope this helps.

Yang

0
source

Source: https://habr.com/ru/post/1736518/


All Articles