我正在使用 DryIoc,这是我第一次使用依赖注入(inject)框架。基本上我需要将所有实现 IService 的类注册为单例。在每个 IService 实现中,我还需要注册所有其他接口(interface)。我想用一条指令来做到这一点。 所以我的第一次尝试是:
container.RegisterMany<IService>(Reuse.Singleton);
我收到 DryIoc.ContainerException Registering abstract implementation type IService 当它应该是具体的。也没有可以使用的 FactoryMethod
经过一些谷歌搜索我发现:
container.RegisterMany(new[] { typeof(IService).Assembly }, type => type.IsAssignableTo(typeof(IService)),
Reuse.Singleton, FactoryMethod.ConstructorWithResolvableArguments);
但是
resolver.ResolveMany<IOtherInterface>();
返回空枚举。
如果我像这样注册每一个实现
container.RegisterMany<Service1>(Reuse.Singleton);
container.RegisterMany<Service2>(Reuse.Singleton);
...
一切正常,但我想有一种方法可以做到这一点,而无需为每个服务实现编写一个寄存器。
最佳答案
它不起作用,因为您过滤的是服务类型(接口(interface))而不是实现类型。
试试这个:
container.RegisterMany(
typeof(IService).Assembly.GetLoadedTypes()
.Where(t => t.IsAssignableTo(typeof(IService))),
t => t.IsInterface, …);
关于c# - RegisterMany 不注册所有接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49716028/