基本上对于像这样的解析器,您必须自己进行多次注册——即,您必须注册“注册的服务列表”,而不是直接注册类型。这有点痛苦。
如果您不需要通过构造函数进行服务注入,我鼓励您只使用内置的,它实际上是 Pretty Good™。
更新:终于找到了我写的一些代码来解决 AutoFac 中的类似问题: var builder = new ContainerBuilder(); var 容器 = 默认值(IContainer);
#region Don't actually read this terrible code
// NB: This garbage exists in order to work around the fact that
// Autofac doesn't support multiple registrations for the same type
// (i.e. GetAllServices), but RxUI relies on it.
var registrations = new Dictionary<Tuple<Type, string>, List<Type>>();
RxApp.ConfigureServiceLocator(
(t, s) => s != null ? container.ResolveNamed(s, t) : container.Resolve(t),
(t, s) => {
var type = typeof(IEnumerable<>).MakeGenericType(t);
var ret = (IEnumerable<Type>)container.ResolveNamed<IEnumerable<Type>>(type.FullName);
return ret.Select(x => Activator.CreateInstance(x)).ToArray();
},
(c, t, s) => {
// NB: This is the hackiest hack of hack to work around a bug in RxUI
if (container != null) return;
var pair = Tuple.Create(t, s);
if (!registrations.ContainsKey(pair)) registrations[pair] = new List<Type>();
registrations[pair].Add(c);
});
foreach (var kvp in registrations) {
if (kvp.Value.Count == 1) {
var reg = builder.RegisterType(kvp.Value[0]).As(kvp.Key.Item1);
if (kvp.Key.Item2 != null) reg.Named(kvp.Key.Item2, kvp.Key.Item1);
} else {
var type = typeof(IEnumerable<>).MakeGenericType(kvp.Key.Item1);
builder.RegisterInstance(kvp.Value).As<IEnumerable<Type>>().Named<IEnumerable<Type>>(type.FullName);
}
}
#endregion