Autofac注销所有实现接口的内容

5
能否在Autofac中注销接口的所有实现?
我的情况是这样的:
我注册了两个模块,一个是默认模块(DefaultModule),如果满足某些条件,我会后续注册一个特定模块(SpecificModule)。
builder.RegisterModule(new DefaultModule());

if (someCondition)
{
   builder.RegisterModule(new SpecificModule());
}

这两个模块注册了一个接口的多个命名实例,我们称之为ISomething。

在DefaultModule的Load函数内部:

builder.RegisterType<DefaultSomething1>().Named<ISomething>("DefaultSomething1").SingleInstance();
builder.RegisterType<DefaultSomething2>().Named<ISomething>("DefaultSomething2").SingleInstance();

在SpecificModule的Load函数中:

builder.RegisterType<SpecificSomething1>().Named<ISomething>("SpecificSomething1").SingleInstance();
builder.RegisterType<SpecificSomething2>().Named<ISomething>("SpecificSomething2").SingleInstance();

当我注册SpecificModule时,我想注销所有先前的ISomething注册,因为它们作为集合被注入到另一个构造函数中。
public SomeClass(IEnumerable<ISomething> somethingCollection)
{
   _somethingCollection = somethingCollection;
}

这可行吗?还是用其他方式更好?

1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接