IEnumerable
Unityで解決したいコンストラクターパラメーターを取り、オブジェクトの配列を注入するクラスがあります。これらの単純なクラスは問題を示しています。
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; }
}
に 4 つを注入したいと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>
ただし、エラー メッセージThe configuration is set to inject an array, but the type IEnumerable`1 is not an array type が表示されます。 コンストラクターのパラメーターを変更するIThing[]
と機能しますが、それはしたくありません。これを機能させるには、Unity 構成に何をする必要がありますか?