假设我有一个定义格式化报告的界面:
public interface IFormatter
{
string Name {get;}
Report Format(InputData data);
}
以及一个为我格式化报告的提供程序的实现:
public FormatterProvider : IFormatterProvider
{
private readonly IList<IFormatter> _formatter;
public FormatterProvider(IList<IFormatter> formatters)
{
_formatters = formatters;
}
public IFormatter GetFormatter(string name){ return _formatters.Where(x => x.Name == name); }
}
我知道我可以使用此注册来注册程序集中的所有格式化程序:
container.Register(
AllTypes.FromAssemblyName("MyCompany.Formatters")
.BasedOn<IFormatter>()
.WithService
.FromInterface()
);
但是,如何注册提供程序,以便将容器中注册的任何格式化程序传递给其构造函数?
最佳答案
使用名为 CollectionResolver 的子解析器。
Container.Kernel.Resolver.AddSubResolver(new CollectionResolver(Container.Kernel, true));
我认为您需要将 IList 更改为 IEnumerable 才能完成这项工作,但不确定。
关于c# - 将组件列表解析为构造函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6656361/