如何确定使用反射基类的泛型参数

Tom*_*ord 1 c# generics reflection

我有以下结构

public class MyClass : MyBaseClass<System.Int32>
{
}
Run Code Online (Sandbox Code Playgroud)

在静态方法中,如果没有实例化新的MyClass实例,我如何获得用于构建具体基类的泛型参数的类型?例如,在上面的示例System.Int32中

Jar*_*Par 5

试试这个

public static Type GetBaseTypeGenericArgument(Type type) {
  return type.BaseType.GetGenericArguments()[0];
}

...
GetBaseTypeGenericArgument(typeof(MyClass));
Run Code Online (Sandbox Code Playgroud)