5

In the example below, I'm trying to understand why the BaseType is not a generic type definition and more generally, why it isn't just equal to the typeof(List<>)

public class MyList<T> : List<T>
{
}

// this is true by definition
typeof(List<>).IsGenericTypeDefinition

// this is false
// also the BaseType.FullName is null ?!?
// also BaseType.IsConstructedGenericType is true ?!?!?
// as such, BaseType != typeof(List<>)
typeof(MyList<>).BaseType.IsGenericTypeDefinition;

EDIT - Here are some useful links on related topics:

Difference between Type.IsGenericTypeDefinition and Type.ContainsGenericParameters

What exactly is an "open generic type" in .NET?

Generics -Open and closed constructed Types

Detect if a generic type is open?

https://learn.microsoft.com/en-us/dotnet/api/system.type.isgenerictype

Difference between IsGenericType and IsGenericTypeDefinition

Suraj
  • 35,905
  • 47
  • 139
  • 250
  • 1
    [This](https://stackoverflow.com/questions/2173107/what-exactly-is-an-open-generic-type-in-net) can give you some ideas – Eldar Dec 02 '19 at 15:18
  • 1
    [This](https://stackoverflow.com/questions/31772922/difference-between-isgenerictype-and-isgenerictypedefinition) could also be useful. – Dalsier Dec 02 '19 at 16:13
  • 1
    Thanks for the links! I'm including those in the original post for reference, along with some others I found. – Suraj Dec 02 '19 at 18:26
  • 2
    Interestingly, the documentation for `Type.BaseType` covers all cases *except* for the case of a generic type definition, so if you want to get technical, the result isn't even properly defined. What is actually returned is an open type `List`, with `T` unbound (i.e. `.GetGenericParameters()[0].IsGenericParameter == true`). This is not unreasonable, but it's still not documented. – Jeroen Mostert Dec 02 '19 at 18:40
  • 1
    @JeroenMostert - I found the same; I read the documentation like 5 times to make sure I wasn't missing anything but it's not addressed! I agree that it's not unreasonable, but it does seems sub-optimal and not as intuitive as returning the generic type definition. – Suraj Dec 05 '19 at 01:27

0 Answers0