2011-07-21 4 views
3

名前が示すように、構造マップは名前付きインスタンスも設定したときにデフォルトインスタンスを返しません。ここで構造マップはデフォルトのものの代わりに名前付きのインスタンスを返します

は私のタイプの登録である:

/// <summary> 
    /// Initializes a new instance of the <see cref="CommandProcessingTypeRegistry"/> class. 
    /// </summary> 
    public CommandProcessingTypeRegistry() 
    { 
    For<ICommandProcessor>().Singleton().Use<CommandCoordinator>(); 

    For<ICommandProcessor>().Singleton().Use<SystemCommandSwitch>().Named(typeof(SystemCommandSwitch).FullName); 
    For<ICommandProcessor>().Singleton().Use<TelephonyCommandSwitch>().Named(typeof(TelephonyCommandSwitch).FullName); 
    For<ICommandProcessor>().Singleton().Use<AudioCommandSwitch>().Named(typeof(AudioCommandSwitch).FullName); 
    For<ICommandProcessor>().Singleton().Use<TetraCommandSwitch>().Named(typeof(TetraCommandSwitch).FullName); 
    For<ICommandProcessor>().Singleton().Use<RadioCommandSwitch>().Named(typeof(RadioCommandSwitch).FullName); 
    For<ICommandProcessor>().Singleton().Use<SnapshotCommandSwitch>().Named(typeof(SnapshotCommandSwitch).FullName); 
    For<ICommandProcessor>().Singleton().Use<TakeNextCommandSwitch>().Named(typeof(TakeNextCommandSwitch).FullName); 
    } 

そして、これは私がインスタンスを要求する方法である:

_commandProcessor = _container.GetInstance<ICommandProcessor>(); // _container is the structuremap IContainer instance 

私は上記の行は私にCommandCoordinatorインスタンスを返すことを好きではなくなりTakeNextCommandSwitchインスタンスが返されます。 ここで何が間違っていますか?

答えて

5

あなたが名前付きインスタンスのために使用するのではなく、追加使用する必要があります。

For<ICommandProcessor>().Singleton().Add<TelephonyCommandSwitch>().Named(typeof(TelephonyCommandSwitch).FullName); 
関連する問題