inversion-of-control - 统一框架: How to Instantiate two classes from the same Interface?

标签 inversion-of-control unity-container

我有一个接口(interface),有 2 个类实现它,我需要加载每个类,但 Unity 有:

m_unityContainer.Resolve()//IGeneric接口(interface)在哪里

我的配置如下:

      <type type="IGeneric" mapTo="ClassA">
      </type>
      <type type="IGeneric" mapTo="ClassB">
      </type>

有什么想法吗?

谢谢

最佳答案

您还可以使用通用接口(interface),如下所示:

public interface IGeneric{}

public interface IGeneric<T> : IGeneric{}

然后对接口(interface)进行类型安全解析:

container.RegisterType<IGeneric<ClassA>, ClassA>();
container.RegisterType<IGeneric<ClassB>, ClassB>();

ClassA classA = container.Resolve<IGeneric<ClassA>>();
ClassB classB = container.Resolve<IGeneric<ClassB>>();

当你沿着这条路走下去时,一些有趣的事情开始发生......

关于inversion-of-control - 统一框架: How to Instantiate two classes from the same Interface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/644501/

相关文章:

asp.net - 如何注册通用存储库的依赖?

c# - 国际奥委会 : How to create objects dynamically

c# - 如何在运行时配置Unity 2.0来拦截INotifyPropertyChanged?

asp.net-mvc - Unity.MVC4惰性<T>在ASP.NET MVC应用程序中不起作用

unity-container - 将同一类型注册到多个接口(interface)

dependency-injection - 带有选定引号的控制反转与依赖注入(inject)——我的理解是否正确?

C# + MongoDB - 不使用 MongoDB 数据类型/属性的 ObjectId

c# - MVCApplication 中对 IoC 容器的静态引用会导致内存泄漏吗?

Log4net 和 Unity 注册

c# - 模拟服务调用对象返回 null