Autofac中如何注册开放泛型注册开放泛型装饰器?我有一个看起来像这样的autofac处理程序的开放式通用注册表。builder.RegisterAssemblyTypes(组件).AsClosedTypesOf(typeof(ICommandHandler)).AsImplementedInterfaces();这工作正常并为封闭类型注册我的所有处理程序。我现在想为所有处理程序注册一个通用装饰器,例如LoggingCommandHandlerDecorator从autofac文档中,您需要命名实现,因此装饰器可以是默认的ICommandHandler。我不确定在注册开放泛型时这是如何工作的。我试过在公开注册中添加一个名字。builder.RegisterAssemblyTypes(组件).AsClosedTypesOf(typeof(ICommandHandler)).Named("commandHandler",typeof(ICommandHandler)).AsImplementedInterfaces();并注册装饰器,但没有快乐。builder.RegisterGenericDecorator(typeof(LoggingCommandHandlerDecorator),typeof(ICommandHandler),fromKey:"commandHandler");任何帮助表示赞赏。根据这里的autofac文档,尝试这个解决方案://用名称注册开放泛型,以便//装饰器可以使用它。builder.RegisterGeneric(typeof(CommandHandler)).Named("commandHandler",typeof(ICommandHandler));//注册通用装饰器,以便它可以包装//已解析的命名generics.builder.RegisterGenericDecorator(typeof(LoggingCommandHandlerDecorator),typeof(ICommandHandler),fromKey:"commandHandler");我做了同样的,没有工作;然后找到解决方案。作者:BobyJohnson-要点C#教程到此为止:如何在Autofac中注册开放通用装饰器以进行开放通用注册?如果分享的内容对你有用,需要进一步了解C#学习教程,希望你多多关注—builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).As(type=>type.GetInterfaces()。Where(interfaceType=>interfaceType.IsClosedTypeOf(typeof(ICommandHandler))).Select(interfaceType=>newKeyedService("commandHandler",interfaceType))).InstancePerLifetimeScope();本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: