C# Type GetGenericTypeDefinition

Description

Type GetGenericTypeDefinition returns a Type object that represents a generic type definition from which the current generic type can be constructed.

Syntax

Type.GetGenericTypeDefinition has the following syntax.


public virtual Type GetGenericTypeDefinition()

Returns

Type.GetGenericTypeDefinition method returns A Type object representing a generic type from which the current type can be constructed.

Example

The following code example creates an instance of a constructed type by using ordinary instance creation and then uses the GetType and GetGenericTypeDefinition methods to retrieve the constructed type and the generic type definition.


using System;/*  w  w w .j  a  v a 2  s.  c  om*/
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        Dictionary<string, Test> d = new Dictionary<string, Test>();

        Type constructed = d.GetType();
        DisplayTypeInfo(constructed);

        Type generic = constructed.GetGenericTypeDefinition();
        DisplayTypeInfo(generic);
    }

    private static void DisplayTypeInfo(Type t)
    {
        Console.WriteLine("Is this a generic type definition? {0}", 
            t.IsGenericTypeDefinition);
        Console.WriteLine("Is it a generic type? {0}", 
            t.IsGenericType);
        Type[] typeArguments = t.GetGenericArguments();
        Console.WriteLine("List type arguments ({0}):", typeArguments.Length);
        foreach (Type tParam in typeArguments)
        {
            Console.WriteLine(tParam);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version