Export (0) Print
Expand All

Type.IsGenericTypeDefinition Property

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)

public virtual bool IsGenericTypeDefinition { get; }
/** @property */
public boolean get_IsGenericTypeDefinition ()

public function get IsGenericTypeDefinition () : boolean

Not applicable.

Property Value

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

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.

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.

using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    private static void DisplayGenericTypeInfo(Type t)
    {
        Console.WriteLine("\r\n{0}", t);

        Console.WriteLine("\tIs this a generic type definition? {0}", 
            t.IsGenericTypeDefinition);

        Console.WriteLine("\tIs it a generic type? {0}", 
            t.IsGenericType);

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

            Console.WriteLine("\tList type arguments ({0}):", 
                typeArguments.Length);

            foreach (Type tParam in typeArguments)
            {
                // If this is a type parameter, display its
                // position.
                //
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }

    public static void Main()
    {
        Console.WriteLine("\r\n--- Display information about a constructed type, its");
        Console.WriteLine("    generic type definition, and an ordinary type.");

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

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

        // Display information for an ordinary type.
        DisplayGenericTypeInfo(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
 */

import System.*;
import System.Reflection.*;
import System.Collections.Generic.*;

public class Test
{
    private static void DisplayGenericTypeInfo(Type t)
    {
        Console.WriteLine("\r\n{0}", t);
        Console.WriteLine("\tIs this a generic type definition? {0}",
            (System.Boolean)t.get_IsGenericTypeDefinition());
        Console.WriteLine("\tDoes it have generic arguments? {0}",
            (System.Boolean)t.get_IsGenericType());
        if (t.get_IsGenericType()) {
            // If this is a generic type, display the type arguments.
            Type typeArguments[] = t.GetGenericArguments();
            Console.WriteLine("\tList type arguments ({0}):",
                (Int32)typeArguments.get_Length());
            for (int iCtr = 0; iCtr < typeArguments.get_Length(); iCtr++) {
                Type tParam = typeArguments[iCtr];
                // If this is a type parameter, display its
                // position.
                
                if (tParam.get_IsGenericParameter()) {
                    Console.WriteLine("\t\t{0}\t(unassigned - parameter "
                        + "position {1})", tParam, 
                        (Int32)tParam.get_GenericParameterPosition());
                }
                else {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    } //DisplayGenericTypeInfo

    public static void main(String[] args)
    {
        Console.WriteLine("\r\n--- Display information about a "
            + "constructed type, its");
        Console.WriteLine("    generic type definition, and an ordinary type.");
        // Create a Dictionary of Test objects, using strings for the
        // keys.       
        Dictionary<String,Test> d = new Dictionary<String,Test>();
        // Display information for the constructed type and its generic
        // type definition.
        DisplayGenericTypeInfo(d.GetType());
        DisplayGenericTypeInfo(d.GetType().GetGenericTypeDefinition());
        // Display information for an ordinary type.
        DisplayGenericTypeInfo(String.class.ToType());
    } //main
} //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
 */

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft