简体   繁体   中英

IOC Container Runtime Resolution

I'm trying to find an IOC container that will allow me to have mapping data for a field stored in a database and resolve the interface or object that needs resolved via a string value pulled from the database.

Most of the examples I have seen are using interfaces hard coded in code, I want the interface that needs to be resolved to be dynamic.

This is what I usually see:

var taskController = container.Resolve<ITaskController>();

This is what I would like to see:

var strTaskController = "ITaskController";
var taskController = container.Resolve(strTaskController);

I'm sure I could look through the documentation for all the IOC containers but I am hoping this is an easy question for someone with more IOC experience.

Using Unity you can do what you're looking for. Basically, if you know the full type name, you can do this first:

var type = Type.GetType("Fully.Qualified.Type.Name");
var resolvedInstance = container.Resolve(type);

EDIT: Based on the comment, here's another approach:

string typeName = "MyTypeName";
var type = container.Registrations.FirstOrDefault(r => r.RegisteredType.Name == typeName);
if(type != null)
{
    var resolvedInstance = container.Resolve(type.RegisteredType);
}

I think this is the answer I am going with.. Managed Extensibility Framework http://msdn.microsoft.com/en-us/library/dd460648.aspx

Got to love it when you find a new framework to find the exact solution to your problem.

您可以使用Castle项目中的IOC容器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM