如何获得typeof通用接口

时间:2015-01-28 12:37:54

标签: c# .net generics reflection

我有

interface ITestInterface<TSource,TDestination>

我想要

void TestFunction(Type t1, Type t2)
{
    var x = typeof(ITestInterface<t1, t2>)
}

genericMethod.Invoke之间有什么区别(this,null) 和调用方法直接,即TestFunction(typeof(Emp),typeof(Dept))。 所以我可以改变功能

void TestFunction<TSource, TDestination>()
{
var x = typeof(ITestInterface<TSource, TDestination>)
}

1 个答案:

答案 0 :(得分:2)

您可以使用MakeGenericType方法构建泛型类型:

var x = typeof(ITestInterface<,>).MakeGenericType(t1, t2);

第一个(typeof(ITestInterface<t1, t2>))无效,因为您传递了两个类型实例,其中type是预期的。他们不一样。泛型是静态类型的,您不能将类型实例指定为泛型参数。

相关问题