autofac的Func 来解析命名服务

鉴于注册服务:

builder.RegisterType().Named("one").As(); builder.RegisterType().Named("two").As(); builder.RegisterType().Named("three").As(); 

我可以通过注入类似Func东西来检索IFoo接口的命名实现吗?

 public class SomeClass(Func foo) { var f = foo("one"); Debug.Assert(f is Foo1); var g = foo("two"); Debug.Assert(g is Foo2); var h = foo("three"); Debug.Assert(h is Foo3); } 

我知道我可以用Meta做,但我不想用它。

您可以像这样注册自己的解析代表:

 builder.Register>(c => { var cc = c.Resolve(); return named => cc.ResolveNamed(named); });