Autofac with Open Generics and Type Specified at Runtime
This is certainly possible with Autofac. At "register time", this is what you basically do:
- Register the open generic type (PassThroughFlattener<>)
- Register any specific types (AddressFlattener)
- Register a method that can be used to resolve an IFlattener based on a input object
At "resolve time", you will do:
- Resolve the method
- Call the method with input parameter(s) to resolve the IFlattener implementation
Here's a (hopefully) working sample:
var openType = typeof(IFlattener<>);
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(PassThroughFlattener<>)).As(openType);
builder.Register<AddressFlattener>().As<IFlattener<Address>>();
builder.Register<Func<object, IFlattener>>(context => theObject =>
{
var closedType =
openType.MakeGenericType(theObject.GetType());
return (IFlattener) context.Resolve(closedType,
new PositionalParameter(0, theObject));
});
var c = builder.Build();
var factory = c.Resolve<Func<object, IFlattener>>();
var address = new Address();
var addressService = factory(address);
Assert.That(addressService, Is.InstanceOfType(typeof(AddressFlattener)));
var anything = "any other data";
var anyService = factory(anything);
Assert.That(anyService, Is.InstanceOfType(typeof(PassThroughFlattener<string>)));
If you don't know type until runtime you can build it using MakeGenericType:
var addressFlattener = _container.Resolve(typeof(IFlattener<>).MakeGenericType(typeof(Address)));