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

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

相关内容

热门资讯

透视线上!大菠萝手游辅助,hh... 透视线上!大菠萝手游辅助,hhpoker是真的还是假的,攻略教程(有挂教学)-哔哩哔哩1、hhpok...
透视肯定!pokemmo脚本,... 透视肯定!pokemmo脚本,wepoker有辅助吗,资料教程(有挂教学)-哔哩哔哩1、wepoke...
透视模拟器!大菠萝免费辅助,w... 透视模拟器!大菠萝免费辅助,wepoker破解工具,法门教程(有挂头条)-哔哩哔哩1、大菠萝免费辅助...
透视安卓版!wepoker有没... 透视安卓版!wepoker有没有机器人,hhpoker脚本下载,课程教程(有挂技巧)-哔哩哔哩wep...
透视安装!wepoker公共底... 透视安装!wepoker公共底牌,pokermaster破解版,模块教程(讲解有挂)-哔哩哔哩1、完...
透视私人局!hhpoker辅助... 透视私人局!hhpoker辅助软件下载,wepoker公共底牌,攻略教程(讲解有挂)-哔哩哔哩1、很...
透视数据!wpk插件,poke... 透视数据!wpk插件,pokerworld软件,方针教程(有挂分析)-哔哩哔哩1、打开软件启动之后找...
透视有挂!wpk私人局辅助是真... 透视有挂!wpk私人局辅助是真的吗,wejoker开挂,演示教程(有挂详细)-哔哩哔哩暗藏猫腻,小编...
透视总结!wepoker黑侠破... 透视总结!wepoker黑侠破解,德州私人局怎么透视,诀窍教程(有挂秘笈)-哔哩哔哩1、超多福利:超...
透视苹果版!德普之星怎么设置埋... 透视苹果版!德普之星怎么设置埋牌,哈糖大菠萝破解器,妙招教程(有挂教学)-哔哩哔哩在进入德普之星怎么...