brucebanner
brucebanner

Reputation: 3

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.

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.

Upvotes: 0

Views: 899

Answers (1)

TylerOhlsen
TylerOhlsen

Reputation: 5578

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.

Upvotes: 1

Related Questions