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

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

相关内容

热门资讯

微扑克辅助挂!微扑克脚本代写,... 您好,微扑克辅助挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款...
微扑克辅助器ios!微扑克德州... 微扑克辅助器ios!微扑克德州专用辅助器游戏平台,(微扑克app)竟然有挂(详细wpk透视辅助教程)...
德扑ai助手!德扑之星系统发牌... 德扑ai助手!德扑之星系统发牌机制,德扑之星决策都是是有挂(详细ai智能教程)关于德扑ai助手的基本...
aapoker辅助工具存在!a... aapoker辅助工具存在!aapoker有手游版吗,(aapoker讲解)总是是真的有挂(详细有外...
微扑克ai辅助器苹果版!微扑克... 微扑克ai辅助器苹果版!微扑克靠谱吗,(微扑克规律)原来是真的有挂(详细系统发牌规律教程)是由北京得...
WePoKe透视挂!wepow... WePoke高级策略深度解析‌;WePoKe透视挂!wepower线上游戏发牌机制规律(wepoke...
微扑克ai机器人!wpk微扑克... 微扑克ai机器人!wpk微扑克真的有辅助插件吗,(微扑克下载)切实存在有挂(详细辅助器ios教程);...
微扑克wpk透视辅助!微扑克有... 1、微扑克wpk透视辅助!微扑克有假吗,(微扑克软件)一直有挂(详细游戏辅助器教程);详细教程。2、...
we辅助poker德之星!wo... 1、we辅助poker德之星!wopoker有没有外卦挂(wepoke辅助挂)竟然存在有挂(详细透明...
wepoke有辅助挂!wepo... WePoker透视辅助工具核心要点解析‌,wepoke有辅助挂!wepoke软件(wepoke游戏)...