如何获取typeof泛型接口

How to get typeof generic interface

我有

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>)
}

可以使用MakeGenericType方法构造泛型:

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

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