【问题标题】:use named mapping type to resolve constructor parameter with Unity使用命名映射类型通过 Unity 解析构造函数参数
【发布时间】:2013-06-08 22:42:38
【问题描述】:

我想保证统一使用命名注册来解析构造函数参数,例如:

我有两种类型的注册:

_container.RegisterType<IMyInterface, Implementation1>("implementation1");
_container.RegisterType<IMyInterface, Implementation2>("implementation2");

当统一使用以下构造函数解析一个类时:

public class Example
{
    Example(IMyInterface args)
    {     
    }
}

我应该如何指定在这种情况下必须使用“implementation2”解决的统一

【问题讨论】:

    标签: inversion-of-control unity-container ioc-container


    【解决方案1】:

    您可以在 Unity 中配置类型注册,以使用以下方法将构造函数参数解析为特定命名类型:

    container.RegisterType<IExample, Example>(
        new InjectionConstructor(new ResolvedParameter<IMyInterface>("implementation2")));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多