注册泛型接口的类:
builder.RegisterAssemblyTypes(typeof(IEventHandler<>).Assembly) .Where(t => t.IsClass && t.GetInterfaces().Any(i=>i.IsGenericType && i.GetGenericTypeDefinition()==typeof(IEventHandler<>))) .AsSelf() .AsImplementedInterfaces();
解析泛型接口:
var handlerType = typeof(IEventHandler<>).MakeGenericType(typeof(TEvent)); //TEvent是泛型 var handlerInstances = Activator.Invoke(typeof(IEnumerable<>).MakeGenericType(handlerType)) as IEnumerable;
Autofac复杂案例