本文介绍了统一解决单个接口的多种实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

public interface IFoo { }

public interface IFoo3 { }

public class Foo4 : IFoo3 { }

public class Foo1 : IFoo { }

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

在客户端:

  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

解决方案2(请参阅注释)给出了一个找不到构造函数的错误.

The resolve 2 (see comment ) gives an error that a constructor could not be found.

我基本上希望它解析为Foo2类.我什至尝试使用parameteroverrides,但这也不起作用.

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

请帮助我.

推荐答案

不带参数调用Resolve将为您提供未命名注册的实例(没有 named 注册) ).

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

不带参数调用ResolveAll会为您提供所有命名注册的实例(不包括 unnamed 注册).

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

您没有IFoo未命名注册.

这篇关于统一解决单个接口的多种实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:51