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.