TypeBuilder.MakeGenericType(Type[]) Method

Definition

Substitutes the elements of an array of types for the type parameters of the current generic type definition, and returns the resulting constructed type.

C#
public override Type MakeGenericType(params Type[] typeArguments);

Parameters

typeArguments
Type[]

An array of types to be substituted for the type parameters of the current generic type definition.

Returns

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

Exceptions

The current type does not represent the definition of a generic type. That is, IsGenericTypeDefinition returns false.

typeArguments is null.

-or-

Any element of typeArguments is null.

The Module property of any element of typeArguments is null.

-or-

The Assembly property of the module of any element of typeArguments is null.

Remarks

Use this method when your emitted code requires a type constructed from the current generic type definition. It is not necessary to call the CreateType method before calling the MakeGenericType method on a TypeBuilder that represents a generic type definition. If the current TypeBuilder does not represent the definition of a generic type, an InvalidOperationException is thrown.

The object returned by this method functions as a placeholder for a constructed generic type in your emitted code. It is an instance of a class derived from Type that has limited capabilities. In particular:

C#
Type t1 = tbldr.MakeGenericType(typeof(string));
Type t2 = tbldr.MakeGenericType(typeof(string));
bool result = t1.Equals(t2);

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1