问题描述
我有接口定义IFoo<TBar> : IFoo
和方法CreateFooUsingBarType(Type barType)
。我需要使用定义TBar
的指定的System.Type
实例使用依赖项注入工具来解决IFoo<TBar>
的方法。不要问我如何到这里。我被困在这些边界中。
示例
public IFoo CreateFooUsingBarType(Type barType)
{
var iocScope = GetScope();
// TODO: Create a System.Type for IFoo<TBar>
// where TBar is barType. Blarghity blargh.
var fooType = (Type)null;
return iocScope.Resolve(fooType);
}
我已经尝试用TypeBuilder来闲逛,但我感觉到这太过杀了。.NET是否会暴露其他API来实现此目标?
预先感谢。
您可以使用makeGenerictype方法:
var fooType = typeof(IFoo<>).MakeGenericType(barType);