为什么是typeof(Child <>).BaseType.IsGenericTypeDefinition false?

Den*_*i35 6 .net c# generics

例:

class Base<T>{}
class Child<T> : Base<T>{}

typeof( Base<> ).IsGenericTypeDefinition; // == true ie. parameterless 
typeof( Child<> ).BaseType.IsGenericTypeDefinition; // == false wtf???

// Eventually 
typeof( Base<> ) != typeof( Child<> ).BaseType;
Run Code Online (Sandbox Code Playgroud)

由于此功能typeof( Child<> ).IsSubclassOf( typeof( Base<> ) )不起作用.

Den*_*i35 1

我在书中找到了一些信息Professional .NET 2.0 Generics,部分Reflection and Generic Inheritance

继承类型间接绑定到子类的 TValue 参数。

我做了一些简单的测试:

  var arg = typeof( Base<> ).GetGenericArguments()[ 0 ];
  Console.WriteLine( arg.DeclaringType ); // Base`1[T]

  arg = typeof( Child<> ).BaseType.GetGenericArguments()[ 0 ];
  Console.WriteLine( arg.DeclaringType ); // Child`1[T]
Run Code Online (Sandbox Code Playgroud)

“无参数”一词让我感到困惑。实际上,即使没有指定泛型参数的类型也有参数。它的类型带有arg.IsGenericParameter == true.

而且这些类型也是可以继承的。与通常情况一样:

typeof( Child<int> ).BaseType == typeof( Base<int> );
Run Code Online (Sandbox Code Playgroud)

与无参数情况相同的方式:

typeof( Base<> ) != typeof( Child<> ).BaseType; // it's something like: Base<T>.T !=  Child<T>.T
Run Code Online (Sandbox Code Playgroud)