指定的文档指出Autofac支持开放泛型和我能够像这样注册和解析的基本情况:Autofac与开放式泛型和类型在运行时
报名:
builder.RegisterGeneric(typeof(PassThroughFlattener<>))
.As(typeof(IFlattener<>))
.ContainerScoped();
解决:
var flattener = _container.Resolve<IFlattener<Address>>();
上面的代码工作得很好。但是,假设我不知道提供给IFlattener类型,直到运行时,我想要做这样的事情:
object input = new Address();
var flattener = (IFlattener)_container.Resolve(typeof(IFlattener<>), new TypedParameter(typeof(IFlattener<>), input.GetType()));
这可能与AutoFac?我从以下使用StructureMap的想法:
http://structuremap.sourceforge.net/Generics.htm
我试图实现在这篇文章中列出的相同目标。
感谢您的快速响应,该工作! – 2009-10-06 12:54:05