Autofac中,RegisterAssemblyOpenGenericTypes和RegisterAssemblyTypeswithAsClosedTypesOf的区别是什么?
创始人
2024-11-12 12:30:23
0

在Autofac中,RegisterAssemblyOpenGenericTypes()方法具有通用的特性,而RegisterAssemblyTypes().AsClosedTypesOf()方法则是用于将已知类型关闭为指定的泛型类型。 例如,在以下示例中,你可以看到如何使用RegisterAssemblyOpenGenericTypes()方法来注册泛型类型,以及如何使用RegisterAssemblyTypes().AsClosedTypesOf()方法通过关闭现有类型来注册指定泛型类型:

using Autofac;
using System.Linq;
using System.Reflection;

public class ExampleModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        // Register all open generic types in a specific assembly as themselves.
        builder.RegisterAssemblyOpenGenericTypes(Assembly.GetExecutingAssembly())
            .AsSelf();

        // Register all types in a specific assembly that close some known type as that type.
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .AsClosedTypesOf(typeof(ILogger<>));
    }
}

以上示例中,使用RegisterAssemblyOpenGenericTypes()方法,注册了当前程序集中的所有开放泛型类型,并将它们都注册为它们自己。这就是使用RegisterAssemblyOpenGenericTypes()方法注册泛型类型的一个示例。

另一方面,使用RegisterAssemblyTypes().AsClosedTypesOf()方法,则可以使用现有的类型,将它们关闭为指定的泛型类型。例如,在示例中将所有的ILogger<>类型都关闭为已知的ILogger类型。

相关内容

热门资讯

透视存在!pokemmo脚本辅... 透视存在!pokemmo脚本辅助器(透视)2025新版教程(2020已更新)(哔哩哔哩)1、poke...
透视脚本!aapoker透视脚... 透视脚本!aapoker透视脚本(透视)免费透视脚本(都是是真的有挂)暗藏猫腻,小编详细说明aapo...
透视计算!哈糖大菠萝挂(透视)... 透视计算!哈糖大菠萝挂(透视)教你攻略(2025已更新)(哔哩哔哩)1、这是跨平台的哈糖大菠萝挂黑科...
透视能赢!德州透视插件(透视)... 透视能赢!德州透视插件(透视)普及教程(2025已更新)(哔哩哔哩)1.德州透视插件 ai辅助创建新...
透视脚本!aapoker辅助器... 透视脚本!aapoker辅助器怎么用(透视)ai插件(都是真的是有挂);1、许多玩家不知道aapok...
透视游戏!xpoker辅助怎么... 透视游戏!xpoker辅助怎么用(透视)解密教程(2024已更新)(哔哩哔哩)透视游戏!xpoker...
透视新版!aapoker怎么选... 透视新版!aapoker怎么选牌(透视)免费透视脚本(总是有挂)1、游戏颠覆性的策略玩法,独创攻略技...
透视辅助!智星德州有脚本吗(透... 透视辅助!智星德州有脚本吗(透视)揭秘攻略(2025已更新)(哔哩哔哩);1、实时智星德州有脚本吗开...
透视脚本!aapoker发牌逻... 透视脚本!aapoker发牌逻辑(透视)透视脚本入口(确实有挂)1、任何aapoker发牌逻辑ai辅...
透视好友!来玩德州破解器(透视... 透视好友!来玩德州破解器(透视)德州论坛(2026已更新)(哔哩哔哩);1、金币登录送、破产送、升级...