C# Type MakeGenericType

Description

Type MakeGenericType substitutes the elements of an array of types for the type parameters of the current generic type definition and returns a Type object representing the resulting constructed type.

Syntax

Type.MakeGenericType has the following syntax.


public virtual Type MakeGenericType(
  params Type[] typeArguments
)

Parameters

Type.MakeGenericType has the following parameters.

  • typeArguments - An array of types to be substituted for the type parameters of the current generic type.

Returns

Type.MakeGenericType method returns A Type representing the constructed type formed by substituting the elements of typeArguments for the type parameters of the current generic type.

Example

The following example uses the MakeGenericType method to create a constructed type from the generic type definition for the Dictionary<TKey, TValue> type.


/*from  www . j av a 2 s. c  om*/
using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {    
        Type generic = typeof(Dictionary<,>);
        Type[] typeArgs = { typeof(string), typeof(Test) };

        Type constructed = generic.MakeGenericType(typeArgs);
    }

}

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