2

我知道您可以在创建LifetimeScope 添加注册,如下所示:

using(var scope = container.BeginLifetimeScope(builder =>  
    {  builder.RegisterType<Override>().As<IService>();
       builder.RegisterModule<MyModule>();
    }))
{
  // The additional registrations will be available
  // only in this lifetime scope.
}

创建后是否可以添加注册LifetimeScopeusing(例如在块内添加注册)

4

1 回答 1

0

您可以访问 的ComponentRegistry属性ILifetimeScope,使用此对象您可以注册新IComponentRegistration的,您可以使用RegistrationBuilder静态方法创建它:

using(var scope = container.BeginLifetimeScope())
{
    var registration = RegistrationBuilder.ForType<ServiceA>()
                                          .As<IService>()
                                          .CreateRegistration();
    scope.ComponentRegistry.Register(registration); 

    scope.Resolve<IService>(); 
}
于 2016-08-10T19:18:30.697 回答