Autofac: Batch registration of open-generic types

前端 未结 2 1025
[愿得一人]
[愿得一人] 2020-12-09 05:10

I got an assembly with many concrete types that implement IHandler, such as the following:

public class MoveCustomerHandler : IH         


        
相关标签:
2条回答
  • 2020-12-09 05:14

    In a similar style to Jim's answer but taking advantage of AsClosedTypesOf:

    Assembly[] assemblies = GetYourAssemblies();
    
    builder.RegisterAssemblyTypes(assemblies)
        .AsClosedTypesOf(typeof(IHandler<>));
    
    0 讨论(0)
  • 2020-12-09 05:20

    You probably want something like this, although I'm not sure how IsAssignable() behaves with open generics.

    Assembly[] assemblies = GetYourAssemblies();
    
    builder.RegisterAssemblyTypes(assemblies)
        .Where(t => t.IsAssignableFrom(typeof(IHandler<>)))
        .AsSelf()
        .AsImplementedInterfaces();
    
    0 讨论(0)
提交回复
热议问题