获取Autofac中接口的所有已注册实现 [英] Get all registered implementations of an interface in Autofac

查看:118
本文介绍了获取Autofac中接口的所有已注册实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 IComponentContext 中获取实现特定接口的已注册 Type 的列表.

I need to get, from an IComponentContext, a list of registered Type's that implement a particular interface.

我不想要这些类型的实际实例,而是我可以可以获取其实例的 Type 的列表.

I don't want actual instances of the types, but rather a list of Type of which I could get instances.

我想使用此列表在消息总线上生成订阅.

I want to use this list to generate subscriptions on a message bus.

如何在Autofac中获得接口的所有注册实现?

How do I get all registered implementations of an interface in Autofac?

推荐答案

我知道了-

var types = scope.ComponentRegistry.Registrations
    .SelectMany(r => r.Services.OfType<IServiceWithType>(), (r, s) => new { r, s })
    .Where(rs => rs.s.ServiceType.Implements<T>())
    .Select(rs => rs.r.Activator.LimitType);

这篇关于获取Autofac中接口的所有已注册实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆