我有一个接口(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/