Difference between Type.IsGenericTypeDefinition and Type.ContainsGenericParameters

前端 未结 3 1227
梦毁少年i
梦毁少年i 2020-12-17 09:05

The System.Type type contains the properties IsGenericTypeDefinition and ContainsGenericParameters. After reading the MSDN documentation I conclude that both pr

相关标签:
3条回答
  • 2020-12-17 09:30

    There is a table under IsGenericType that tries to highlight some differences:

    The IsGenericTypeDefinition property is true.

    Defines a generic type. A constructed type is created by calling the MakeGenericType method on a Type object that represents a generic type definition and specifying an array of type arguments.

    or:

    The ContainsGenericParameters property is true.

    Examples are a generic type that has unassigned type parameters, a type that is nested in a generic type definition or in an open constructed type, or a generic type that has a type argument for which the ContainsGenericParameters property is true.

    So they're not precisely the same.

    0 讨论(0)
  • 2020-12-17 09:50

    Type.ContainsGenericParameters is recursive:

    var genericList = typeof(List<>);
    var listOfSomeUnknownTypeOfList = genericList.MakeGenericType(genericList);
    listOfSomeUnknownTypeOfList.IsGenericTypeDefinition;  // => false
    listOfSomeUnknownTypeOfList.ContainsGenericParameters; // => true
    

    What happens here is that listOfSomeUnknownTypeOfList is not a generic type definition itself because its type parameter is known to be a List<T> for some T. However, since the type of listOfSomeUnknownTypeOfList is not exactly known (because its type argument is a type definition) ContainsGenericParameters is true.

    0 讨论(0)
  • 2020-12-17 09:54

    ContainsGenericParameters is a recursive version of IsGenericTypeDefinition.

    typeof(List<Func<>>).IsGenericTypeDefinition is false.

    0 讨论(0)
提交回复
热议问题