简体   繁体   English

统一解决单个接口的多种实现

[英]Resolve multiple implementation of a single interface in unity

I have the following scenario : 我有以下情况:

public interface IFoo { }

public interface IFoo3 { }

public class Foo4 : IFoo3 { }

public class Foo1 : IFoo { }

public class Foo2 : IFoo
{  
    Foo2 (IFoo object1, IFoo3 object2)   
}

on the client side : 在客户端:

  IUnityContainer container = new UnityContainer();
  container.RegisterType<IFoo, Foo1>("Foo1");
  container.RegisterType<IFoo3, Foo4>();
  container.RegisterType<IFoo, Foo2>("Foo2");

  IFoo3 obj = container.Resolve<IFoo3>(); //Resolve 1

  IFoo obj2 = container.Resolve<IFoo>(); //Resolve 2

The resolve 2 (see comment ) gives an error that a constructor could not be found. 解决方案2(请参阅注释)给出了一个错误,即找不到构造函数。

I basically want it to resolve to Foo2 class. 我基本上希望它解析为Foo2类。 I even tried using parameteroverrides but that did not work either. 我什至尝试使用parameteroverrides,但这也不起作用。

Please help me on this. 请帮我。

Calling Resolve with no parameters gets you an instance of the unnamed registration (no named registrations). 调用不带任何参数的Resolve可获取未命名注册(无命名注册)的实例。

Calling ResolveAll with no parameters gets you instances of all named registrations (does not include the unnamed registration). 调用不带参数的ResolveAll会为您提供所有已命名注册的实例(不包括未命名注册)。

You have no unnamed registration of IFoo . 您没有IFoo 未命名注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM