C# 为具有已知类型T的泛型接口构造System.Type
问题描述 我有一个接口定义C# 为具有已知类型T的泛型接口构造System.Type,c#,reflection,inversion-of-control,C#,Reflection,Inversion Of Control,问题描述 我有一个接口定义IFoo:IFoo和一个方法createfooousingbartype(Type barType)。我需要使用依赖项注入工具解析IFoo的方法,给定指定的系统。键入定义TBar的实例。别问我是怎么来到这里的。我被困在这些界限之内 示例 public IFoo CreateFooUsingBarType(Type barType) { var iocScope = GetScope(); // TODO: Create a System.Type fo
IFoo:IFoo
和一个方法createfooousingbartype(Type barType)
。我需要使用依赖项注入工具解析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);
}
public IFoo CreateFoooUsingBarType(类型barType)
{
var iocScope=GetScope();
//TODO:创建系统。为IFoo键入
//其中TBar是barType.Blarghity blargh。
var fooType=(Type)null;
返回iocScope.Resolve(fooType);
}
我试过和他混日子,但我觉得这样做太过分了。NET是否公开了不同的API来实现这一点
提前感谢。您可以使用以下方法:
var fooType=typeof(IFoo);
您可以使用以下方法:
var fooType=typeof(IFoo);
Doh!这么简单。谢谢请注意,
是指IFoo
而不是IFoo
.Doh!这么简单。谢谢请注意,
需要引用IFoo
,而不是IFoo
。
var fooType = typeof(IFoo<>).MakeGenericType(barType);