Autofac complex cases

Registration class generic interface:

builder.RegisterAssemblyTypes(typeof(IEventHandler<>).Assembly)
                .Where(t => t.IsClass && t.GetInterfaces().Any(i=>i.IsGenericType && i.GetGenericTypeDefinition()==typeof(IEventHandler<>)))
                .AsSelf()
                .AsImplementedInterfaces();

Parsing generic interface:

var handlerType = typeof(IEventHandler<>).MakeGenericType(typeof(TEvent));  //TEvent是泛型
var handlerInstances = Activator.Invoke(typeof(IEnumerable<>).MakeGenericType(handlerType)) as IEnumerable;

Guess you like

Origin www.cnblogs.com/fanfan-90/p/12100239.html