IOC Container Runtime Resolution

冷暖自知 提交于 2019-12-11 11:35:24

问题


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.


回答1:


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);
}



回答2:


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.




回答3:


You can use the IOC container from the Castle project.



来源:https://stackoverflow.com/questions/3718175/ioc-container-runtime-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!