Autofac Keyed/Named 方法不提供服务映射函数的重载

Autofac Keyed/Named methods do not provide overload with service mapping func

As 方法提供了一个带有 Func<Type, Type> serviceMapping 参数的重载,但 KeyedNamed 方法没有。它们分别只提供Func<Type, object> serviceKeyMappingFunc<Type, string> serviceNameMapping参数。

但是,我想向 RegisterAssemblyTypes 注册一组类型,对所有类型使用相同的键,但使用由类型本身决定的不同接口。我期待找到一个方法重载,例如 Keyed(object serviceKey, Func<Type, Type> serviceMapping)Keyed(Func<Type, object> serviceKeyMapping, Func<Type, Type> serviceMapping).

这是 API 设计中的疏忽吗?还是我遗漏了什么?

API好像没有这个功能。但是,您可以将 As(Func<Type, Service> serviceMapping) 重载与 KeyedService 对象一起使用。

例如

builder.RegisterAssemblyTypes(typeof(Parent).Assembly)
        .Where(t => t.IsAssignableTo<ICommon>())
        .As(t => new KeyedService(keyObject, t.GetType().GetInterfaces()[0]));

KeyedServiceAutofac.Core 命名空间中。没有 NamedService 对象,但您可以使用 KeyedServicestring