0

我有父类动物和两个子类狗和猫。我正在使用 Autofac 进行依赖注入。我想根据配置文件中指定的条件注册 Dog 和 Cat。

我想在我的模块类中使用以下代码。

If Dog mentioned in Config file then builder.RegisterType<Dog>().As<Animal>().PropertiesAutowired().SingleInstance(); If cat mentioned in Config file then builder.RegisterType<Cat>().As<Animal>().PropertiesAutowired().SingleInstance();

请建议相同。

4

1 回答 1

0

像这样:

if (ConfigurationManager.AppSettings["Animal"] == "Dog")
    builder.RegisterType<Dog>().As<Animal>().PropertiesAutowired().SingleInstance();

if (ConfigurationManager.AppSettings["Animal"] == "Cat")
    builder.RegisterType<Cat>().As<Animal>().PropertiesAutowired().SingleInstance();
于 2016-08-01T10:36:05.797 回答