解决了IEnumerable< T>使用Unity [英] Resolving IEnumerable<T> with Unity

查看:694
本文介绍了解决了IEnumerable< T>使用Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否团结自动解析的IEnumerable< T>

让我们说我有这个构造函数的类:

Let's say I have a class with this constructor:

public CoalescingParserSelector(IEnumerable<IParserBuilder> parserBuilders)

和我在容器配置单个IParserBuilder实例:

and I configure individual IParserBuilder instances in the container:

container.RegisterType<IParserSelector, CoalescingParserSelector>();
container.RegisterType<IParserBuilder, HelpParserBuilder>();
container.RegisterType<IParserBuilder, SomeOtherParserBuilder>();

我可以做这项工作,而无需执行的自定义实现的IEnumerable&LT; IParserBuilder&GT;

var selector = container.Resolve<IParserSelector>();

到目前为止,我还没有能够给前preSS这没有简单的方法,但我仍然对团结向上斜坡,所以我可能错过了一些东西。

So far I haven't been able to express this in any simple way, but I'm still ramping up on Unity so I may have missed something.

推荐答案

原来,这其实是非常简单的事情:

It turns out that this is actually awfully simple to do:

container.RegisterType<IEnumerable<IParserBuilder>, IParserBuilder[]>();

统一的本地了解到阵列,所以我们只需要在枚举映射到相同类型的数组。

Unity natively understands arrays, so we just need to map the enumerable to an array of the same type.

这篇关于解决了IEnumerable&LT; T&GT;使用Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆