Autofac自动注入Named的依赖项

注入三个接口:

builder.RegisterType<Foo1>().Named<IFoo>("one").As<IFoo>();
builder.RegisterType<Foo2>().Named<IFoo>("two").As<IFoo>();
builder.RegisterType<Foo3>().Named<IFoo>("three").As<IFoo>();

注入引用:

builder.Register<Func<string, IFoo>>(c =>
    {
        var cc = c.Resolve<IComponentContext>();
        return named => cc.Resolve<IFoo>(named);
    });

解决方案很精妙啊~

来自stackoverflow:http://stackoverflow.com/questions/2888621/autofacs-funct-to-resolve-named-service

你可能感兴趣的:(autofac)