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类型。

相关内容

热门资讯

推荐透视!wepoker有脚本... 推荐透视!wepoker有脚本吗,wepoker养号规律,原来有辅助app(哔哩哔哩)1、进入游戏-...
分享透视!aapoker安装包... 分享透视!aapoker安装包怎么使用,aapoker辅助怎么用,真是一直都是有辅助插件(哔哩哔哩)...
普及透视!有哪些免费的wpk作... 普及透视!有哪些免费的wpk作比弊码,wpk透视怎么安装,一直真的是有辅助插件(哔哩哔哩)1、透视辅...
专业透视!hhpoker有没有... 专业透视!hhpoker有没有作比弊挂,hhpoker怎么破解,一直是有辅助插件(哔哩哔哩)该软件可...
普及透视!aapoker辅助工... 普及透视!aapoker辅助工具安全吗,aa poker透视软件,竟然存在有辅助技巧(哔哩哔哩)一、...
科普透视!wepoker免费脚... 科普透视!wepoker免费脚本咨询,如何下载wepoker安装包,一贯一直都是有辅助软件(哔哩哔哩...
开挂透视!德普之星怎么开辅助,... 开挂透视!德普之星怎么开辅助,德普之星辅助工具如何打开,一贯一直总是有辅助教程(哔哩哔哩)1、公共底...
开挂透视!hhpoker可以控... 开挂透视!hhpoker可以控制吗,wepoker软件安装包,好像存在有辅助方法(哔哩哔哩)1、该软...
必备透视!aapoker破解侠... 必备透视!aapoker破解侠是真的吗,aapoker怎么提高中牌率,都是存在有辅助教程(哔哩哔哩)...
揭幕透视!hhpoker怎么开... 揭幕透视!hhpoker怎么开透视,hhpkoer辅助挂是真的吗,真是一直总是有辅助app(哔哩哔哩...