简体   繁体   中英

autofac multiple registrations based on generics type

I have a class that recieves a generic type:

RequestHandler<T>

Now I want to register this class in Autofac multiple times based on the generic type. Something like this:

builder.RegisterType<RequestHandler<Request1>>().As<RequestHandler<T>>();
builder.RegisterType<RequestHandler<Request2>>().As<RequestHandler<T>>();
builder.RegisterType<RequestHandler<Request3>>().As<RequestHandler<T>>();
builder.RegisterType<RequestHandler<Request4>>().As<RequestHandler<T>>();

I know that the syntax is incorrect but I hope you get the idea.

so is this even possible? if so how?

您可以使用RegisterGeneric映射打开的通用类型,如下所示:

builder.RegisterGeneric(typeof(RequestHandler<>)).As(typeof(RequestHandler<>));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM