Вот одно решение, которое я нашел, но оно немного наф.Вам нужен класс-обертка, чтобы помочь 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();
}
}
Затем вы можете настроить Unity для внедрения одного из них, используя идею EnumberableThing
Этана.
<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
и внедрить его?