autofac's Func<T> to resolve named service

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.

Answers


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);
    });

Need Your Help

Is there a DOM event that fires when an HTML select element is closed?

javascript jquery html html5 dom

I'm looking for a DOM event that I can listen to with JavaScript for when a select element that has been opened (but no options changed) is then closed by clicking off the select element, somewhere (

How to implement depth first search for graph with non-recursive aprroach

algorithm graph depth-first-search non-recursive

Well, I have spent lots of time on this issue. However, I only can find solutions with non-recursive methods for a tree: Non recursive for tree, or recursive method for the graph, Recursive for gra...