IOC容器运行时分辨率容器、时分、辨率、IOC

由网友(风华正茂╰)分享简介:我试图找到一个IOC容器,这将让我有映射数据存储在数据库中的字段和解决需要通过从数据库中抽取一个字符串值,解决了接口或对象。 I'm trying to find an IOC container that will allow me to have mapping data for a field stored i...

我试图找到一个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);
}
阅读全文

相关推荐

最新文章