autofac's Func<T> to resolve named service
Posted
by ppiotrowicz
on Stack Overflow
See other posts from Stack Overflow
or by ppiotrowicz
Published on 2010-05-22T15:17:47Z
Indexed on
2010/05/22
15:20 UTC
Read the original article
Hit count: 164
Given registered services:
builder.RegisterType<Foo1>().Named<IFoo>("one").As<IFoo>();
builder.RegisterType<Foo2>().Named<IFoo>("two").As<IFoo>();
builder.RegisterType<Foo3>().Named<IFoo>("three").As<IFoo>();
Can I retrieve named implementations of IFoo interface by injecting something like Func<string, IFoo
> ?
public class SomeClass(Func<string, IFoo> 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);
}
I know I can do it with Meta<>, but I don't want to use it.
© Stack Overflow or respective owner