2011-01-11 9 views
1

私はUnityアプリケーションを実行していて、完璧に動作していますが、現在のところ、すべてのコンパイル警告に対処してコードをクリーンアップしています。Unityソリューションの不要なMicrosoft.Practices.Unity.Configuration.ContainerElement.Configureメソッドを置き換えるにはどうすればよいですか?

私は理由時代遅れMicrosoft.Practices.Unity.Configuration.ContainerElement.Configureメソッドの次のコードに警告を得る:私はUnityConfigurationSection.Configureと交換したいと思います

var map = new ExeConfigurationFileMap { ExeConfigFilename = GetConfigFolderForFile("unity.config") }; 
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
var section = (UnityConfigurationSection)config.GetSection(SectionName); 

if (section != null) 
{ 

    var container = new UnityContainer(); 

    foreach (ContainerElement containerElement in section.Containers) 
    { 
     containerElement.Configure(container); 
    } 

    Container = container; // Set the main container 
} 

をメソッドを使用することはできますが、オブジェクト階層の異なるレベルにあるために同等であることはわかりません。

私が試してみた:

var map = new ExeConfigurationFileMap { ExeConfigFilename = GetConfigFolderForFile("unity.config") }; 
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
var section = (UnityConfigurationSection)config.GetSection(SectionName); 

if (section != null) 
{ 
    IUnityContainer container = new UnityContainer(); 

    container = section.Configure(container); 

    Container = container; // Set the main container 
} 
をそれがnull参照で倒れます。

旧式のメソッドの使用を排除するためにコードをどのように更新する必要がありますか?

答えて

1

私はこれを動作させることができました。秘密は、各コンテナ要素を設定するには、コンテナ名をとりConfigureメソッドのオーバーロードを使用していた:時代遅れContainer.Configure方法をやっていたとして、それは同じことをやっているかのように

foreach (ContainerElement containerElement in section.Containers) 
{ 
    container = section.Configure(container, containerElement.Name); 
} 

今のところ、これは見えます - すべてがあります期待どおりに動作します。

1

configにセクションに名前がない場合は、デフォルトとしてそれを取ると、あなたは名前を指定する必要はありません。

<containers> 
    <container> 
    <types> 
     <type type="IObject" mapTo="Object" /> 
    </types> 
    </container> 
</containers> 
関連する問題