The documentation states that Autofac supports open generics, and I can register and resolve in the base case as follows:
Check in:
builder.RegisterGeneric(typeof(PassThroughFlattener<>)) .As(typeof(IFlattener<>)) .ContainerScoped();
Resolve:
var flattener = _container.Resolve<IFlattener<Address>>();
The above code works fine. However, assuming that I will not know the type provided by IFlattener before execution, I want to do something like this:
object input = new Address(); var flattener = (IFlattener)_container.Resolve(typeof(IFlattener<>), new TypedParameter(typeof(IFlattener<>), input.GetType()));
Is this possible with AutoFac? I got an idea from the following using StructureMap:
http://structuremap.sourceforge.net/Generics.htm
I am trying to achieve the same goal outlined in this article.
dependency-injection ioc-container autofac
Page Brooks
source share