Compartir a través de


Type.IsGenericType Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a value indicating whether the current type is a generic type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable ReadOnly Property IsGenericType As Boolean
public virtual bool IsGenericType { get; }

Property Value

Type: System.Boolean
true if the current type is a generic type; otherwise, false.

Remarks

Use the IsGenericType property to determine whether a Type object represents a generic type. Use the ContainsGenericParameters property to determine whether a Type object represents an open constructed type or a closed constructed type.

NoteNote:

The IsGenericType property returns false if the immediate type is not generic. For example, an array whose elements are of type A<int> (A(Of Integer) in Visual Basic) is not itself a generic type.

The following table summarizes the invariant conditions for common terms used in generic reflection.

Term

Invariant

generic type definition

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.

MakeGenericType can be called only on generic type definitions.

Any generic type definition is a generic type (the IsGenericType property is true), but the converse is not true.

generic type

The IsGenericType property is true.

Can be a generic type definition, an open constructed type, or a closed constructed type.

Note that an array type whose element type is generic is not itself a generic type. The same is true of a Type object representing a pointer to a generic type.

open constructed type

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.

It is not possible to create an instance of an open constructed type.

Note that not all open constructed types are generic. For example, an array whose element type is a generic type definition is not generic, and a pointer to an open constructed type is not generic.

closed constructed type

The ContainsGenericParameters property is false.

When examined recursively, the type has no unassigned generic parameters.

generic type parameter

The IsGenericParameter property is true.

The ContainsGenericParameters property is true.

In a generic type definition, a placeholder for a type that will be assigned later.

generic type argument

Can be any type, including a generic type parameter.

Type arguments are specified as an array of Type objects passed to the MakeGenericType method when creating a constructed generic type. If instances of the resulting type are to be created, the ContainsGenericParameters property must be false for all the type arguments.

The following code example and table illustrate some of these terms and invariants. The Derived class is of particular interest because its base type is a constructed type that has a mixture of types and type parameters in its type argument list.

Public Class Base(Of T, U)
End Class
Public Class Derived(Of V) 
    Inherits Base(Of String, V)
    Public F As G(Of Derived(Of V))
    Public Class Nested
    End Class
End Class
Public Class G(Of T)
End Class 
public class Base<T,U> {}
public class Derived<V> : Base<String,V> 
{
    public G<Derived<V>> F;
    public class Nested {}
}
public class G<T> {}
generic <typename T> public ref class G {};
generic <typename T, typename U> public ref class Base {};
generic <typename V> public ref class Derived : Base<String^,V> {
public:
    G<Derived<V>^>^ F;
    ref class Nested {};
};

The following table shows examples that use and build on the classes Base, Derived, and G. When the C++ and C# code is the same, only one entry is shown.

Example

Invariants

Derived(Of V)

Derived<V>

For this type:

IsGenericType is true.

IsGenericTypeDefinition is true.

ContainsGenericParameters is true.

Base(Of String, V)

Base<String,V>

Base<String^,V>

For this type:

IsGenericType is true.

IsGenericTypeDefinition is false.

ContainsGenericParameters is true.

Dim d() As Derived(Of Integer)

Derived<int>[] d;

array<Derived<int>^>^ d;

For the type of variable d:

IsGenericType is false because d is an array.

IsGenericTypeDefinition is false.

ContainsGenericParameters is false.

T, U, and V (everywhere they appear)

IsGenericParameter is true.

IsGenericType is false because there is no way to constrain a type parameter to generic types.

IsGenericTypeDefinition is false.

ContainsGenericParameters is true because T, U, and V are themselves generic type parameters. This does not imply anything about type arguments that are assigned to them later.

The type of field F

IsGenericType is true.

IsGenericTypeDefinition is false because a type has been assigned to to the type parameter of G. Note that this is equivalent to having called the MakeGenericType method.

ContainsGenericParameters is true because the type of field F has a type argument that is an open constructed type. The constructed type is open because its type argument (that is, Base) is a generic type definition. This illustrates the recursive nature of the IsGenericType property.

The nested class Nested

IsGenericType is true, even though the Nested class has no generic type parameters of its own, because it is nested in a generic type.

IsGenericTypeDefinition is true. That is, you can call the MakeGenericType method and supply the type parameter of the enclosing type, Derived.

ContainsGenericParameters is true because the enclosing type, Derived, has generic type parameters. This illustrates the recursive nature of the ContainsGenericParameters property.

Examples

The following code example displays the value of the IsGenericType, IsGenericTypeDefinition, IsGenericParameter, and ContainsGenericParameters properties for the types described in the Remarks section. For explanations of the property values, see the accompanying table in Remarks.

Imports System.Reflection

' 
Public Class Base(Of T, U)
End Class

Public Class Derived(Of V)
   Inherits Base(Of String, V)

   Public F As G(Of Derived(Of V))

   Public Class Nested
   End Class
End Class

Public Class G(Of T)
End Class

Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Get the generic type definition for Derived, and the base
      ' type for Derived.
      '
      Dim tDerived As Type = GetType(Derived(Of ))
      Dim tDerivedBase As Type = tDerived.BaseType

      ' Declare an array of Derived(Of Integer), and get its type.
      '
      Dim d(0) As Derived(Of Integer)
      Dim tDerivedArray As Type = d.GetType()

      ' Get a generic type parameter, the type of a field, and a
      ' type that is nested in Derived. Notice that in order to
      ' get the nested type it is necessary to either (1) specify
      ' the generic type definition Derived(Of ), as shown here,
      ' or (2) specify a type parameter for Derived.
      '
      Dim tT As Type = GetType(Base(Of ,)).GetGenericArguments()(0)
      Dim tF As Type = tDerived.GetField("F").FieldType
      Dim tNested As Type = GetType(Derived(Of ).Nested)

      DisplayGenericType(outputBlock, tDerived, "Derived(Of V)")
      DisplayGenericType(outputBlock, tDerivedBase, "Base type of Derived(Of V)")
      DisplayGenericType(outputBlock, tDerivedArray, "Array of Derived(Of Integer)")
      DisplayGenericType(outputBlock, tT, "Type parameter T from Base(Of T)")
      DisplayGenericType(outputBlock, tF, "Field type, G(Of Derived(Of V))")
      DisplayGenericType(outputBlock, tNested, "Nested type in Derived(Of V)")

   End Sub

   Sub DisplayGenericType(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal t As Type, ByVal caption As String)

      outputBlock.Text &= vbLf & caption & vbCrLf
      outputBlock.Text &= String.Format("    Type: {0}", t) & vbCrLf

      outputBlock.Text &= String.Format(vbTab & "            IsGenericType: {0}", _
          t.IsGenericType) & vbCrLf
      outputBlock.Text &= String.Format(vbTab & "  IsGenericTypeDefinition: {0}", _
          t.IsGenericTypeDefinition) & vbCrLf
      outputBlock.Text &= String.Format(vbTab & "ContainsGenericParameters: {0}", _
          t.ContainsGenericParameters) & vbCrLf
      outputBlock.Text &= String.Format(vbTab & "       IsGenericParameter: {0}", _
          t.IsGenericParameter) & vbCrLf

   End Sub

End Module

' This code example produces the following output:
'
'Derived(Of V)
'    Type: Derived`1[V]
'                    IsGenericType: True
'          IsGenericTypeDefinition: True
'        ContainsGenericParameters: True
'               IsGenericParameter: False
'
'Base type of Derived(Of V)
'    Type: Base`2[System.String,V]
'                    IsGenericType: True
'          IsGenericTypeDefinition: False
'        ContainsGenericParameters: True
'               IsGenericParameter: False
'
'Array of Derived(Of Integer)
'    Type: Derived`1[System.Int32][]
'                    IsGenericType: False
'          IsGenericTypeDefinition: False
'        ContainsGenericParameters: False
'               IsGenericParameter: False
'
'Type parameter T from Base(Of T)
'    Type: T
'                    IsGenericType: False
'          IsGenericTypeDefinition: False
'        ContainsGenericParameters: True
'               IsGenericParameter: True
'
'Field type, G(Of Derived(Of V))
'    Type: G`1[Derived`1[V]]
'                    IsGenericType: True
'          IsGenericTypeDefinition: False
'        ContainsGenericParameters: True
'               IsGenericParameter: False
'
'Nested type in Derived(Of V)
'    Type: Derived`1+Nested[V]
'                    IsGenericType: True
'          IsGenericTypeDefinition: True
'        ContainsGenericParameters: True
'               IsGenericParameter: False
using System;
using System.Reflection;

public class Base<T, U> { }

public class Derived<V> : Base<string, V>
{
   public G<Derived<V>> F;

   public class Nested { }
}

public class G<T> { }

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Get the generic type definition for Derived, and the base
      // type for Derived.
      //
      Type tDerived = typeof(Derived<>);
      Type tDerivedBase = tDerived.BaseType;

      // Declare an array of Derived<int>, and get its type.
      //
      Derived<int>[] d = new Derived<int>[0];
      Type tDerivedArray = d.GetType();

      // Get a generic type parameter, the type of a field, and a
      // type that is nested in Derived. Notice that in order to
      // get the nested type it is necessary to either (1) specify
      // the generic type definition Derived<>, as shown here,
      // or (2) specify a type parameter for Derived.
      //
      Type tT = typeof(Base<,>).GetGenericArguments()[0];
      Type tF = tDerived.GetField("F").FieldType;
      Type tNested = typeof(Derived<>.Nested);

      DisplayGenericType(outputBlock, tDerived, "Derived<V>");
      DisplayGenericType(outputBlock, tDerivedBase, "Base type of Derived<V>");
      DisplayGenericType(outputBlock, tDerivedArray, "Array of Derived<int>");
      DisplayGenericType(outputBlock, tT, "Type parameter T from Base<T>");
      DisplayGenericType(outputBlock, tF, "Field type, G<Derived<V>>");
      DisplayGenericType(outputBlock, tNested, "Nested type in Derived<V>");
   }

   public static void DisplayGenericType(System.Windows.Controls.TextBlock outputBlock, Type t, string caption)
   {
      outputBlock.Text += String.Format("\n{0}", caption) + "\n";
      outputBlock.Text += String.Format("    Type: {0}", t) + "\n";

      outputBlock.Text += String.Format("\t            IsGenericType: {0}",
          t.IsGenericType) + "\n";
      outputBlock.Text += String.Format("\t  IsGenericTypeDefinition: {0}",
          t.IsGenericTypeDefinition) + "\n";
      outputBlock.Text += String.Format("\tContainsGenericParameters: {0}",
          t.ContainsGenericParameters) + "\n";
      outputBlock.Text += String.Format("\t       IsGenericParameter: {0}",
          t.IsGenericParameter) + "\n";
   }
}

/* This code example produces the following output:

Derived<V>
    Type: Derived`1[V]
                    IsGenericType: True
          IsGenericTypeDefinition: True
        ContainsGenericParameters: True
               IsGenericParameter: False

Base type of Derived<V>
    Type: Base`2[System.String,V]
                    IsGenericType: True
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: False

Array of Derived<int>
    Type: Derived`1[System.Int32][]
                    IsGenericType: False
          IsGenericTypeDefinition: False
        ContainsGenericParameters: False
               IsGenericParameter: False

Type parameter T from Base<T>
    Type: T
                    IsGenericType: False
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: True

Field type, G<Derived<V>>
    Type: G`1[Derived`1[V]]
                    IsGenericType: True
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: False

Nested type in Derived<V>
    Type: Derived`1+Nested[V]
                    IsGenericType: True
          IsGenericTypeDefinition: True
        ContainsGenericParameters: True
               IsGenericParameter: False
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.