Resolve multiple implementation of a single interface in unity

点点圈 提交于 2019-12-08 12:30:18

问题


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.

I basically want it to resolve to Foo2 class. I even tried using parameteroverrides but that did not work either.

Please help me on this.


回答1:


Calling Resolve with no parameters gets you an instance of the unnamed registration (no named registrations).

Calling ResolveAll with no parameters gets you instances of all named registrations (does not include the unnamed registration).

You have no unnamed registration of IFoo.



来源:https://stackoverflow.com/questions/22662645/resolve-multiple-implementation-of-a-single-interface-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!