我有一门课需要 IEnumerable
我想用 Unity 解析并注入(inject)对象数组的构造函数参数。这些简单的类说明了这个问题。
public interface IThing
{
int Value { get; }
}
public class SimpleThing : IThing
{
public SimpleThing()
{
this.Value = 1;
}
public int Value { get; private set; }
}
public class CompositeThing : IThing
{
public CompositeThing(IEnumerable<IThing> otherThings)
{
this.Value = otherThings.Count();
}
public int Value { get; private set; }
}
说我要注入(inject)四个
SimpleThing
进入 CompositeThing
.我已经尝试了以下 Unity 配置的几种变体。<alias alias="IThing" type="TestConsoleApplication.IThing, TestConsoleApplication" />
<alias alias="SimpleThing" type="TestConsoleApplication.SimpleThing, TestConsoleApplication" />
<alias alias="CompositeThing" type="TestConsoleApplication.CompositeThing, TestConsoleApplication" />
<container>
<register type="IThing" mapTo="SimpleThing" name="SimpleThing" />
<register type="IThing" mapTo="CompositeThing" name="CompositeThing">
<constructor>
<param name="otherThings">
<array>
<dependency type="SimpleThing"/>
<dependency type="SimpleThing"/>
<dependency type="SimpleThing"/>
<dependency type="SimpleThing"/>
</array>
</param>
</constructor>
</register>
</container>
但是我收到错误消息 配置设置为注入(inject)数组,但类型 IEnumerable`1 不是数组类型。 如果我将构造函数参数更改为
IThing[]
它有效,但我不想那样做。我需要对我的 Unity 配置做些什么才能使其正常工作?
最佳答案
这是我找到的一个解决方案,但它有点笨拙。您需要一个包装类来帮助 Unity 识别数组是 IEnumerable
.
public class ArrayWrapper<T> : IEnumerable<T>
{
public ArrayWrapper(T[] things)
{
this.things = things;
}
private IEnumerable<T> things;
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return this.things.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.things.GetEnumerator();
}
}
然后,您可以使用 Ethan 的
EnumberableThing
将 Unity 配置为注入(inject)其中之一。主意。<alias alias="IThing" type="TestConsoleApplication.IThing, TestConsoleApplication" />
<alias alias="SimpleThing" type="TestConsoleApplication.SimpleThing, TestConsoleApplication" />
<alias alias="CompositeThing" type="TestConsoleApplication.CompositeThing, TestConsoleApplication" />
<alias alias="EnumerableThing" type="System.Collections.Generic.IEnumerable`1[[TestConsoleApplication.IThing, TestConsoleApplication]], mscorlib"/>
<alias alias="ThingArrayWrapper" type="TestConsoleApplication.ArrayWrapper`1[[TestConsoleApplication.IThing, TestConsoleApplication]], TestConsoleApplication"/>
<container>
<register type="EnumerableThing" mapTo="ThingArrayWrapper">
<constructor>
<param name="things">
<array>
<dependency type="SimpleThing"/>
<dependency type="SimpleThing"/>
<dependency type="SimpleThing"/>
<dependency type="SimpleThing"/>
</array>
</param>
</constructor>
</register>
<register type="IThing" mapTo="SimpleThing" name="SimpleThing" />
<register type="IThing" mapTo="CompositeThing" name="CompositeThing">
<constructor>
<param name="otherThings" dependencyType="EnumerableThing" />
</constructor>
</register>
</container>
就像我说的那样,当你想要另一个
CompositeThing
时有点笨拙和尴尬三,五,六件事等。当然,Unity 应该能够识别出一个数组是
IEnumberable
。并注入(inject)它?
关于dependency-injection - 如何配置 Unity 为 IEnumerable 注入(inject)数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8519709/