Autofac:如何获取考虑到所有父级范围的所有注册信息?
创始人
2024-11-12 13:00:13
0

您可以使用Autofac的IComponentContext接口来获取所有考虑到所有父级范围的注册信息。下面是一个示例代码:

using System;
using Autofac;

public class Program
{
    public static void Main()
    {
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterType().As();
        containerBuilder.RegisterType().As();

        var container = containerBuilder.Build();

        using (var scope = container.BeginLifetimeScope())
        {
            var componentContext = scope.Resolve();
            var serviceRegistrations = componentContext.ComponentRegistry.RegistrationsFor(new TypedService(typeof(IService)));

            foreach (var registration in serviceRegistrations)
            {
                Console.WriteLine(registration.Activator.LimitType);
            }
        }
    }
}

public interface IService
{
}

public class Service1 : IService
{
}

public class Service2 : IService
{
}

在上面的示例中,我们首先创建了一个ContainerBuilder并注册了两个服务(Service1Service2)实现了IService接口。然后我们使用containerBuilder.Build()创建了一个IContainer实例。接下来,我们使用container.BeginLifetimeScope()创建了一个新的生命周期范围,并在使用完后自动释放。在生命周期范围内,我们可以使用scope.Resolve()来获取IComponentContext实例。然后,我们使用componentContext.ComponentRegistry.RegistrationsFor(new TypedService(typeof(IService)))方法来获取所有注册了IService类型的组件。最后,我们使用foreach循环遍历所有注册信息,并打印出注册的类型。

运行上述代码,输出将是:

Service1
Service2

这证明我们成功获取了考虑到所有父级范围的所有注册信息。

相关内容

热门资讯

玩家攻略推荐!约局吧能不能开挂... 玩家攻略推荐!约局吧能不能开挂,约局吧如何查看是否有挂,扑克教程(有挂神器);支持多人共享记分板与复...
记者揭秘!wepoker辅助分... 1、记者揭秘!wepoker辅助分析器,拱趴大菠萝有什么挂,科技教程(有挂技巧)。2、拱趴大菠萝有什...
今日焦点!拱趴大菠萝挂,xpo... 今日焦点!拱趴大菠萝挂,xpoker辅助工具,扑克教程(有挂辅助)是一款可以让一直输的玩家,快速成为...
一起来讨论!wepoker怎么... 1、一起来讨论!wepoker怎么发冤家牌,哈糖大菠萝怎么开挂,透明挂教程(有挂软件)(UU pok...
2024教程!wepoker作... 2024教程!wepoker作弊视频,哈糖大菠萝挂法,可靠教程(有挂教程)1、玩家可以在哈糖大菠萝挂...
推荐一款!微扑克微乐辅助,we... 推荐一款!微扑克微乐辅助,wejoker私人辅助软件,科技教程(有挂教程);致您一封信;亲爱wejo...
玩家必看教程!wepoker买... 玩家必看教程!wepoker买钻石有用吗,werplan怎么作弊,AI教程(有挂教程);致您一封信;...
如何分辨真伪!wepoker怎... 此外,数据分析德州(wepoker怎么设置透视)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
必备教程!xpoker辅助神器... 必备教程!xpoker辅助神器,wepoker辅助器有哪些功能,科技教程(有挂方法);致您一封信;亲...
科技通报!aapoker破解侠... 科技通报!aapoker破解侠是真的吗,epoker透视,介绍教程(有挂神器)关于aapoker破解...