structuremap 确实返回命名实例而不是默认实例

标签 structuremap named-instance

正如标题所说,当我还配置了命名实例时,structuremap 不会返回默认实例。

这是我的类型注册:

/// <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 实例。 我在这里做错了什么?

最佳答案

您需要对命名实例使用添加而不是使用:

For<ICommandProcessor>().Singleton().Add<TelephonyCommandSwitch>().Named(typeof(TelephonyCommandSwitch).FullName);

关于structuremap 确实返回命名实例而不是默认实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6773236/

相关文章:

c# - 如何检索 StructureMap 中所有命名实例的名称

sql-server - SQL Server,将命名实例转换为默认实例?

sql-server - 如何在 Ubuntu 上创建 Microsoft SQL Server 的命名实例

c# - 带有 IP 地址的 SQL Server 实例名称

c# - 如何在 StructureMap 中注入(inject)实现某个通用接口(interface)的对象列表?

inversion-of-control - StructureMap - 覆盖命名实例的构造函数参数

c# - Autowiring 属性注入(inject)

c# - 结构图 ObjectFactory.GetAllInstances<IHandle<TEvent>>()