c# - 是否可以使用 Autofac 程序集扫描为多个键注册一种类型?
问题描述
我正在尝试MyClass
在 AutoFac 中注册多个键(即 3、4、5)的类型(即)。所以, componentContext.ResolveKeyed<T>(3)
,componentContext.ResolveKeyed<T>(4)
和componentContext.ResolveKeyed<T>(5)
所有返回MyClass
实例。我不确定如何根据Keyed<IMyClass>()
类型而不是值来执行此操作。
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(type => type.IsAssignableTo<IMyClass>())
.Keyed<IMyClass>(type => type.GetCustomAttribute<MyCustomAttribute>().VALUES)
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
[MyCustomAttribute(3, 4, 5)]
class MyClass : IMyClass { }
class MyCustomAttribute : Attribute {
public int[] VALUES { get; set; }
public MyCustomAttribute(params int[] values) {
this.Values = values;
}
}
解决方案
这在 Autofac 核心中不受支持,但您可以轻松地创建一个扩展方法来为您完成它。
public static IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle>
Keyed<TService>(
this IRegistrationBuilder<object, ScanningActivatorData, DynamicRegistrationStyle> registration,
Func<Type, object[]> serviceKeyMapping)
{
var serviceType = typeof(TService);
return registration
.AssignableTo(serviceType)
.As(t => serviceKeyMapping(t).Select(key => new KeyedService(key, serviceType)));
}
它应该大致可以替代您正在尝试做的事情。
通过使用现有的扩展方法作为许多工作示例的起点,Autofac 很容易以这种方式进行扩展。我放在这里的是对现有Keyed<T>
程序集扫描扩展的一个非常小的修改。由于不可能涵盖每个用例,因此这是填补此类空白的“官方解决方案”。
推荐阅读
- linux - ubuntu crontab 芹菜节拍
- php - 如何访问数组中的对象属性
- keras - 在 Keras 中实现架构的建议
- wpf - XAML 中各个 Point 资源的 PointCollection 资源?
- java - org.xml.sax.SAXParseException:cvc-elt.1.a:找不到元素的声明
- html - col-sm-12 在智能手机上没有 100% 的宽度 - 我做错了什么?
- c - 如何在 Fedora 中编译 -static ncursesw
- reactjs - 将 React 包含为 webpack 外部结果会导致找不到模块“react”
- javascript - 车轮事件 PreventDefault 不取消车轮事件
- python - Visual Studio Code - Code Runner:'python' 不是内部或外部命令、可运行程序或批处理文件