Is it possible to remove an existing registration from Autofac container builder?

前端 未结 2 1161
囚心锁ツ
囚心锁ツ 2020-12-18 19:05

Something along those lines:

builder.RegisterType().As();
builder.RegisterType().As();
builder.DeRegis         


        
2条回答
  •  醉酒成梦
    2020-12-18 19:55

    This cannot be done directly using the ContainerBuilder, unless you start over with a new one. Mind you, having first built a container you should be able to construct a new container filtering away unwanted types and reusing the registrations from the first container. Like this:

    ...
    var container = builder.Build();
    
    builder = new ContainerBuilder();
    var components = container.ComponentRegistry.Registrations
                        .Where(cr => cr.Activator.LimitType != typeof(LifetimeScope))
                        .Where(cr => cr.Activator.LimitType != typeof(MyType));
    foreach (var c in components)
    {
        builder.RegisterComponent(c);
    }
    
    foreach (var source in container.ComponentRegistry.Sources)
    {
        cb.RegisterSource(source);
    }
    
    container = builder.Build();
    

    This is hardly very elegant but it works. Now, if you could elaborate on why you want to do this, perhaps there is a better way.

提交回复
热议问题