【发布时间】:2016-10-26 03:00:01
【问题描述】:
我有多种报告类型,每种都接受某种类型的配置:
interface IConfig { ... }
interface IReport { ... }
class ConfigA : IConfig { ... }
class ConfigB : IConfig { ... }
class ReportA : IReport
{
public ReportA (ConfigA config)
{ ... }
}
class ReportB : IReport
{
public ReportB (ConfigB config)
{ ... }
}
如何配置 Unity 容器以通过传递的构造函数参数类型解析 IReport?
var reportA = unityContainer.Resolve<IReport>(new ConfigA()); // Should be ReportA
var reportB = unityContainer.Resolve<IReport>(new ConfigB()); // Should be ReportB
我知道可以创建一种工厂,其中包含类型映射,但在这里我想依赖 Unity 并让我的对象不被容器感知。
【问题讨论】:
标签: c# dependency-injection unity-container