IOC容器运行时分辨率 [英] IOC Container Runtime Resolution

查看:156
本文介绍了IOC容器运行时分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个IOC容器,这将让我有映射数据存储在数据库中的字段和解决需要通过从数据库中抽取一个字符串值,解决了接口或对象。

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.

大多数我所看到的是使用接口的硬盘codeD在code中的例子,我想需要解决的界面是动态的。

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.

这是我平时看到的:

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

这是我希望看到的:

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

我敢肯定,我可以看看通过对所有IOC容器的文档,但我希望这是一个简单的问题的人有更多国际奥委会的经验。

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

编辑:基于注释,这里的另一种方法:

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

这篇关于IOC容器运行时分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆