2016-08-10 37 views
2

我知道,你可以添加註冊一個LifetimeScope是這樣創建的:是否可以在Autofac 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. 
} 

是否有可能的LifetimeScope之後添加註冊創建? (添加註冊的using塊例如內)

+0

你能描述你想要做什麼? –

回答

0

可以訪問ILifetimeScopeComponentRegistry屬性,這個對象可以註冊新IComponentRegistration,你可以使用RegistrationBuilder創建靜態方法:

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

    scope.Resolve<IService>(); 
} 
相關問題