Unity Container配置问题

时间:2009-09-17 15:15:18

标签: c# .net unity-container

我正在使用Unity DI容器。在配置文件中,我将以下类型指定为:

<type type="Interfaces.ILogger,Interfaces" 
 mapTo = "ConcreateClasses.ConsoleLogger,ConcreateClasses" />

我的理解是我的项目中应该引用接口dll和ConcreteClasses dll以使其工作。

但我想做的是不在设计时引用具体的实现类。我希望通过指定ConcreteClasses dll的路径在运行时加载它们。

有办法做到这一点吗?

由于

2 个答案:

答案 0 :(得分:3)

您无需在项目中引用具体的实现程序集,只需将其与配置文件放在同一文件夹中,或者从GAC中获取。

使用具体实现引用其他程序集 CONVENIENT ,以便Visual Studio将DLL的副本放在项目的结果BIN文件夹中,从而使查找变得微不足道。

答案 1 :(得分:1)

你可以通过反思来做到这一点:

Assembly a = Assembly.LoadFrom("pathToDll");
Type interfaceType = typeof(Interfaces.ILogger);
Type implementingType = a.GetTypes.Where(t => t.IsAssignableTo(interfaceType)).First(); //add any other constraints to decide mapping

container.RegisterType(interfaceType, implementingType);
相关问题