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

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

Something along those lines:

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


        
2条回答
  •  暖寄归人
    2020-12-18 19:35

    Peter L.'s probably got the most straightforward option.

    To get around the problem altogether, can you modify the way you're discovering components to filter them in advance of registration? It does seem like there must be an approach that gets around this... It also might be a challenge further down the track to work out which components to keep vs. which to remove.

    A more involved approach is to override IEnumerable support to filter out the the things you don't want. I.e. copy and modify this code to create a FilteredCollectionSource that excludes the components you don't want.

    var elements = c.ComponentRegistry.RegistrationsFor(elementTypeService);
    

    would become:

    var elements = c.ComponentRegistry.RegistrationsFor(elementTypeService)
        .Where(reg => /* not a duplicate */);
    

    If you add your FilteredCollectionSource to the builder using RegisterSource() it will should get used instead of the built-in one.

提交回复
热议问题