Skip to content

TypeParameterBuilder

Create generic type parameter declarations with constraints.

See also: Emit Overview | TypeBuilder | MethodBuilder


Factory Methods

Method Description
For(string name) Create a type parameter (e.g., "T", "TResult")

Constraints

tp.WithConstraint("IDisposable")
tp.AsClass()       // where T : class
tp.AsStruct()      // where T : struct
tp.AsNotNull()     // where T : notnull
tp.WithNewConstraint()  // where T : new()

Constraints can be combined:

TypeParameterBuilder.For("T")
    .AsClass()
    .WithConstraint("IDisposable")
    .WithNewConstraint()
// → where T : class, IDisposable, new()

Example

// On TypeBuilder
TypeBuilder.Class("Repository")
    .AddTypeParameter(TypeParameterBuilder.For("T")
        .AsClass()
        .WithNewConstraint())

// On MethodBuilder
MethodBuilder.For("Convert")
    .AddTypeParameter(TypeParameterBuilder.For("TSource"))
    .AddTypeParameter(TypeParameterBuilder.For("TTarget")
        .WithConstraint("IConvertible"))

Properties

tp.Name           // string
tp.HasConstraints // bool