Type.IsGenericTypeDefinition Property

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

Gets a value indicating whether the current Type represents a generic type definition, from which other generic types can be constructed.

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

Syntax

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

Property Value

Type: System.Boolean
true if the Type object represents a generic type definition; otherwise, false.

Remarks

A generic type definition is a template from which other types can be constructed. For example, from the generic type definition G<T> (expressed in C# syntax; G(Of T) in Visual Basic or generic <typename T> ref class G in C++) you can construct and instantiate the type G<int> (G(Of Integer) in Visual Basic), by calling the MakeGenericType method with a generic argument list containing the Int32 type. Given a Type object representing this constructed type, the GetGenericTypeDefinition method gets the generic type definition back again.

Use the IsGenericTypeDefinition property to determine whether you can create new types from the current type. If the IsGenericTypeDefinition property returns true, you can call the MakeGenericType method to create new generic types.

For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks.

Examples

The following example displays information about a type, including whether or not it is a generic type definition. Information is displayed for a constructed type, for its generic type definition, and for an ordinary type.

Imports System.Reflection
Imports System.Collections.Generic

Public Class Example
   Private Shared Sub DisplayGenericTypeInfo(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal t As Type)
      outputBlock.Text &= vbCrLf & t.ToString() & vbCrLf

      outputBlock.Text &= vbTab & "Is this a generic type definition? " _
          & t.IsGenericTypeDefinition & vbCrLf

      outputBlock.Text &= vbTab & "Is it a generic type? " _
          & t.IsGenericType & vbCrLf

      If t.IsGenericType Then
         ' If this is a generic type, display the type arguments.
         '
         Dim typeArguments As Type() = t.GetGenericArguments()

         outputBlock.Text &= vbTab & "List type arguments (" _
             & typeArguments.Length & "):" & vbCrLf

         For Each tParam As Type In typeArguments
            ' If this is a type parameter, display its position.
            '
            If tParam.IsGenericParameter Then
               outputBlock.Text &= vbTab & vbTab & tParam.ToString() _
                   & vbTab & "(unassigned - parameter position " _
                   & tParam.GenericParameterPosition & ")"
            Else
               outputBlock.Text &= vbTab & vbTab & tParam.ToString() & vbCrLf
            End If
         Next tParam
      End If

   End Sub


   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= String.Format(vbCrLf & "--- Display information about a constructed type, its") & vbCrLf
      outputBlock.Text &= String.Format("    generic type definition, and an ordinary type.") & vbCrLf

      ' Create a Dictionary of Example objects, using strings for the
      ' keys.       
      Dim d As New Dictionary(Of String, Example)()

      DisplayGenericTypeInfo(outputBlock, d.GetType())
      DisplayGenericTypeInfo(outputBlock, d.GetType().GetGenericTypeDefinition())

      ' Display information for an ordinary type.
      DisplayGenericTypeInfo(outputBlock, GetType(String))

   End Sub 'Main
End Class 'Test

' This example produces the following output:
'
'--- Display information about a constructed type, its
'    generic type definition, and an ordinary type.
'
'System.Collections.Generic.Dictionary[System.String, Test]
'        Is this a generic type definition? False
'        Is it a generic type? True
'        List type arguments (2):
'                System.String
'                Test
'
'System.Collections.Generic.Dictionary[TKey,TValue]
'        Is this a generic type definition? True
'        Is it a generic type? True
'        List type arguments (2):
'                TKey    (unassigned - parameter position 0)
'                TValue  (unassigned - parameter position 1)
'
'System.String
'        Is this a generic type definition? False
'        Is it a generic type? False
using System;
using System.Reflection;
using System.Collections.Generic;

public class Example
{
   private static void DisplayGenericTypeInfo(System.Windows.Controls.TextBlock outputBlock, Type t)
   {
      outputBlock.Text += String.Format("\r\n{0}", t) + "\n";

      outputBlock.Text += String.Format("\tIs this a generic type definition? {0}",
          t.IsGenericTypeDefinition) + "\n";

      outputBlock.Text += String.Format("\tIs it a generic type? {0}",
          t.IsGenericType) + "\n";

      if (t.IsGenericType)
      {
         // If this is a generic type, display the type arguments.
         //
         Type[] typeArguments = t.GetGenericArguments();

         outputBlock.Text += String.Format("\tList type arguments ({0}):",
             typeArguments.Length) + "\n";

         foreach (Type tParam in typeArguments)
         {
            // If this is a type parameter, display its
            // position.
            //
            if (tParam.IsGenericParameter)
            {
               outputBlock.Text += String.Format("\t\t{0}\t(unassigned - parameter position {1})",
                   tParam,
                   tParam.GenericParameterPosition) + "\n";
            }
            else
            {
               outputBlock.Text += String.Format("\t\t{0}", tParam) + "\n";
            }
         }
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format("\r\n--- Display information about a constructed type, its") + "\n";
      outputBlock.Text += String.Format("    generic type definition, and an ordinary type.") + "\n";

      // Create a Dictionary of Example objects, using strings for the
      // keys.       
      Dictionary<string, Example> d = new Dictionary<string, Example>();

      // Display information for the constructed type and its generic
      // type definition.
      DisplayGenericTypeInfo(outputBlock, d.GetType());
      DisplayGenericTypeInfo(outputBlock, d.GetType().GetGenericTypeDefinition());

      // Display information for an ordinary type.
      DisplayGenericTypeInfo(outputBlock, typeof(string));
   }
}

/* This example produces the following output:

--- Display information about a constructed type, its
    generic type definition, and an ordinary type.

System.Collections.Generic.Dictionary[System.String,Test]
        Is this a generic type definition? False
        Is it a generic type? True
        List type arguments (2):
                System.String
                Test

System.Collections.Generic.Dictionary[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey    (unassigned - parameter position 0)
                TValue  (unassigned - parameter position 1)

System.String
        Is this a generic type definition? False
        Is it a generic type? 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.