2016-06-11 7 views
2

xml configを使用して抽象クラスのすべての子オブジェクトのリストをコンストラクタに挿入する方法を知りたいと思います。c#Unityコンテナコンストラクタリストの注入<childs>

例えば:

Class Test 
{ 
public Test(List<A> instances) // So this list should have instance of B & C 
{ 

} 
} 

abstract Class A 
{ 

} 

Class B: A 
{ 

} 

Class C: A 
{ 

} 

感謝!

+0

BとCはAを継承していますか? –

+0

おっと!修正されました。ありがとう。 – Deepak

答えて

1

あなたはIList<A>Testのコンストラクタの引数の型を変更する場合は、次の設定を使用することができます。

<configuration> 
    <configSections> 
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/> 
    </configSections> 
    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> 
    <namespace name="System.Collections.Generic" /> 
    <namespace name="ConsoleApplication1" /> 
    <assembly name="ConsoleApplication1" /> 
    <container> 
     <register type="A" mapTo="B" name="B" /> 
     <register type="A" mapTo="C" name="C" /> 
     <register type="IList`1[[A]]" mapTo="A[]" /> 
    </container> 
    </unity> 
</configuration> 

だから、トリックはA[]IList<A>インタフェースをマッピングすることです - 詳細については、this questionを参照してください。

+0

ありがとうStop-cran。 TestコンストラクタがB&Cでリストを取得するように、今これを解決しますか? – Deepak

+0

私は今それを解決することができ、それはリストとB&Cインスタンスで正しく渡されています。ありがとうstop-cran。 – Deepak

関連する問題