.NET API Documentation

Generic Type Parameter Builder

Represents a generic type parameter in a generic type definition being created dynamically.

Namespace:

System.Reflection.Emit

Assembly:

System.Private.CoreLib

Inheritance Hierarchy:

Remarks

The GenericTypeParameterBuilder class is used to define generic type parameters for generic types or methods when using the reflection emit API. You obtain instances of this class by calling the DefineGenericParameters method on a TypeBuilder object (for generic types) or on a MethodBuilder object (for generic methods).

When defining generic type parameters, you can specify constraints such as:

Use the methods of the GenericTypeParameterBuilder class to set these constraints after obtaining the builder.

Constructors

This class does not have public constructors. Instances are obtained through other methods.

Methods

SetBaseType(Type baseType)

Specifies the base type constraint for the generic type parameter.

Parameters:

  • baseType: The Type that the generic type parameter must derive from.
SetInterfaceConstraints(params Type[] interfaceConstraints)

Specifies the interface constraints for the generic type parameter.

Parameters:

  • interfaceConstraints: An array of Type objects representing the interfaces that the generic type parameter must implement.
SetGenericParameterAttributes(GenericParameterAttributes attributes)

Sets the generic parameter attributes, such as whether it is a reference type, value type, or has a constructor.

Parameters:

  • attributes: A GenericParameterAttributes enumeration value specifying the attributes.

Example

// Example: Creating a generic class with a type parameter
using System;
using System.Reflection;
using System.Reflection.Emit;

public class GenericClassExample
{
    public static void Main(string[] args)
    {
        // Define the assembly
        AssemblyName assemblyName = new AssemblyName("MyDynamicAssembly");
        AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect);

        // Define the module
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");

        // Define a generic type builder
        TypeBuilder genericTypeBuilder = moduleBuilder.DefineType("MyGenericType`1");

        // Define a generic type parameter
        GenericTypeParameterBuilder[] typeParameters = genericTypeBuilder.DefineGenericParameters("T");
        GenericTypeParameterBuilder typeParameterT = typeParameters[0];

        // Set constraints for the type parameter T
        typeParameterT.SetGenericParameterAttributes(GenericParameterAttributes.ReferenceType | GenericParameterAttributes.NotNullableValueTypeConstraint); // Example: Must be a reference type
        typeParameterT.SetBaseType(typeof(object)); // Default base type
        typeParameterT.SetInterfaceConstraints(typeof(IDisposable)); // Must implement IDisposable

        // Define a constructor for the generic type
        ConstructorBuilder constructorBuilder = genericTypeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

        // Create the generic type
        Type genericType = genericTypeBuilder.CreateType();

        Console.WriteLine($"Dynamically created generic type: {genericType.FullName}");

        // Example of using the generic type (requires further definition of fields/methods)
        // Type constructedType = genericType.MakeGenericType(typeof(string));
        // Console.WriteLine($"Constructed type: {constructedType.FullName}");
    }
}