【发布时间】:2019-08-09 06:00:12
【问题描述】:
单界面:IDoSomething {...}
两个类实现该接口:
ClassA : IDoSomething {...}
ClassB : IDoSomething {...}
一个类使用这些类中的任何一个。
public class DummyClass(IDoSomething doSomething) {...}
没有 Autofac 的代码:
{
....
IDoSomething myProperty;
if (type == "A")
myProperty = new DummyClass (new ClassA());
else
myProperty = new DummyClass (new ClassB());
myProperty.CallSomeMethod();
....
}
是否可以使用 Autofac 实现类似的功能?
提前致谢,
【问题讨论】:
-
参见文档,Named and Keyed Services部分
-
伊戈尔是对的。你想要一个密钥服务。
标签: autofac