我有一个 Autofac 容器,我希望能够检索所有注册的服务类型(不是实现类型,而是它们注册为的类型)。
我怎样才能从一个IComponentContext
?
你可以使用这个:
var services =
context.ComponentRegistry.Registrations.SelectMany(x => x.Services)
.OfType<IServiceWithType>()
.Select(x => x.ServiceType);
我是如何解决的
var alldb = (from r in MasterDataFactory.Container.ComponentRegistry.Registrations
let s = r.Services.Where(i => i is KeyedService).Select(i => i as KeyedService).FirstOrDefault()
where s!=null && s.ServiceType==typeof(ISomeInterface)
select s).ToList();