ppiotrowicz ppiotrowicz - 1 year ago 94
C# Question

autofac's Func<T> to resolve named service

Given registered services:


Can I retrieve named implementations of
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
, but I don't want to use it.

Answer Source

You could register your own resolving delegate like this:

builder.Register<Func<string, IFoo>>(c =>
        var cc = c.Resolve<IComponentContext>();
        return named => cc.ResolveNamed<IFoo>(named);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download