Is it possible to inject a list of resolved objects into a constructor using Autofac?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 00:38:22
nemesv

Autofac supports the IEnumerable<T> as a relationship type:

For example, when Autofac is injecting a constructor parameter of type IEnumerable<ITask> it will not look for a component that supplies IEnumerable<ITask>. Instead, the container will find all implementations of ITask and inject all of them.

So change your constructor to:

public Detector(IEnumerable<IRecognizer> recognizers)
{
    this.Recognizers = new List<IRecognizer>(recognizers);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!