C# Autofac:解析类型的所有实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1406148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:10:52  来源:igfitidea点击:

Autofac: Resolve all instances of a Type

c#autofac

提问by crowleym

Given the following registrations

鉴于以下注册

builder.Register<A>().As<I>();
builder.Register<B>().As<I>();
builder.Register<C>().As<I>();

var container = builder.Build();

I am looking to resolve all instances of type I as a IEnumerable(Array or Collection it doesn't matter).

我希望将 I 类型的所有实例解析为IEnumerable(数组或集合无关紧要)。

In Windsor I would have written the following.

在温莎,我会写以下内容。

foreach(I i in container.ResolveAll<I>())
{
 ...
}

I am migrating from Windsor to Autofac 1.4.4.561 but can't see the equivalent syntax.

我正在从 Windsor 迁移到 Autofac 1.4.4.561,但看不到等效的语法。

采纳答案by Philip Rieck

For current versons of Autofac: ( 2.0+, so anything you should be using today)

对于当前版本的 Autofac:(2.0+,所以你今天应该使用的任何东西)

You register multiple ILoggers(for example):

您注册多个ILoggers(例如):

var builder = new ContainerBuilder();

builder.Register<ConsoleLogger>()
  .As<ILogger>();

builder.Register<EmailLogger>()
  .As<ILogger>()
  .PreserveExistingDefaults(); //keeps console logger as the default

Then get all ILoggers:

然后得到所有ILogger的:

var loggers = container.Resolve<IEnumerable<ILogger>>();

You don't need to do anything special, just ask for an IEnumerable<T>of the desired type. Autofac has collection support out of the box, along with other adaptersthat can wrap your components with additional functionality.

你不需要做任何特别的事情,只需要求一个IEnumerable<T>所需的类型。Autofac 具有开箱即用的集合支持,以及其他可以为您的组件包装附加功能的适配器

This is the same usage as the pre-2.x ImplicitCollectionSupportModule, but baked right in.

这与 2.x 之前的 ImplicitCollectionSupportModule 的用法相同,但刚好融入其中。

For old versions (0.X - 1.4)

对于旧版本 (0.X - 1.4)

Two ways:

两种方式:

1) Use the collection registration

1)使用收藏注册

var builder = new ContainerBuilder();
builder.RegisterCollection<ILogger>()
  .As<IEnumerable<ILogger>>();

builder.Register<ConsoleLogger>()
  .As<ILogger>()
  .MemberOf<IEnumerable<ILogger>>();

builder.Register<EmailLogger>()
  .As<ILogger>()
  .MemberOf<IEnumerable<ILogger>>();

Then:

然后:

var loggers = container.Resolve<IEnumerable<ILogger>>();

which gives you an IEnumerable.

它为您提供了一个 IEnumerable。

or 2) You can use the ImplicitCollectionSupport module, which will make the code work like newer versions of Autofac:

或 2) 您可以使用 ImplicitCollectionSupport 模块,这将使代码像较新版本的 Autofac 一样工作:

builder.RegisterModule(new ImplicitCollectionSupportModule());
builder.Register(component1).As<ILogger>;
builder.Register(component2).As<ILogger>;

Then resolve a collection of ILogger rather than looking for resolving all.

然后解析 ILogger 的一个集合,而不是寻找所有的解析。

var loggers = container.Resolve<IEnumerable<ILogger>>();

which gives you an IEnumerable, again.

这再次为您提供了一个 IEnumerable。

回答by Nicholas Blumhardt

An update for the sake of the new (2.x) version. All you need now is:

为了新的 (2.x) 版本而进行的更新。你现在需要的是:

container.Resolve<IEnumerable<I>>();

There's no longer a need for RegisterCollection()or ImplicitCollectionSupportModule- this functionality comes out of the box.

不再需要RegisterCollection()ImplicitCollectionSupportModule- 此功能是开箱即用的。