Skip to content

[API Proposal]: Add API to support emitting constructed generic types from varying reflection stacks. #126182

Description

@teo-tsirpanis

Background and motivation

#125612 exposed a feature gap in PersistedAssemblyBuilder when used alongside MetadataLoadContext as suggested. Because a generic type from an MLC cannot be specialized with type parameters outside the MLC, type instantiations like MyTypeFromMLC<T> where T is a TypeBuilder of a just-created type, are not possible at least in some scenarios.

Both runtime reflection and Reflection.Emit support constructing generic types with foreign Type instances, which makes me believe that MLC should too. However, because MLC is defined outside of CoreLib or the shared framework, this cannot happen without new APIs. TypeBuilder already has APIs to construct member references of constructed generic types, but not a public API to construct these generic types (runtime reflection and Reflection.Emit got away with this by using regular Type.MakeGenericType, because they are defined in CoreLib). I am proposing to add one.

API Proposal

namespace System.Reflection.Emit;

public class TypeBuilder
{
+    static Type MakeGenericType(Type genericTypeDefinition, Type[] typeArguments);
}

API Usage

MetadataLoadContext mlc;
ModuleBuilder mb;

Type baseClassT = mlc.LoadAssemblyFromName("MyDependency").GetType("MyDependency.BaseClass`1", true)!;
TypeBuilder tb = mb.DefineType("MyType", TypeAttributes.Public);
Type baseClassOfMyType = TypeBuilder.MakeGenericType(baseClassT, [tb]);
mb.SetParent(baseClassOfMyType);

The above is one possible use, but MetadataLoadContext's MakeGenericType will be updated to make use of this new API, such that we can define baseClassOfMyType with baseClassT.MakeGenericType(tb). On earlier frameworks, MLC will throw a PlatformNotSupportedException.

Alternative Designs

We could have MLC define its own ConstructedGenericType subclass of Type, but some places in Reflection.Emit expect instances of TypeBuilder or TypeBuilderInstantiation, the latter of which is internal and needs the proposed API for user code to create. This restriction could be relaxed, but I'm not sure if it's a good idea.

We could also only add this API and instruct people to use it, without affecting MLC's "closed universe"ness, but I'm not sure I like this idea, given the behavior of other reflection stacks, and the migration effort for developers.

Risks

No response

Activity

  1. added
    api-suggestion
    on Mar 27, 2026
  2. added
    untriaged
    on Mar 27, 2026
  3. dotnet-policy-service commented on Mar 27, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-reflection-emit
    See info in area-owners.md if you want to be subscribed.

  4. jkotas commented on Mar 27, 2026

    @jkotas
    Member

    Why is Type.MakeGenericSignatureType not sufficient?

  5. teo-tsirpanis commented on Mar 27, 2026

    @teo-tsirpanis
    ContributorAuthor

    The linked issue's repro did use MakeGenericSignatureType, but failed with:

    Unhandled exception. System.NotSupportedException: This method is not supported on signature types.
       at System.Reflection.SignatureType.GetAttributeFlagsImpl()
       at System.Type.get_IsInterface()
       at System.Reflection.Emit.TypeBuilderImpl.SetParentCore(Type parent)
       at Program.<Main>$(String[] args)
    

    We could fix this by making more APIs work on signature types and relaxing some Reflection.Emit APIs to work with Type instances of any origin, but people are already using MakeGenericType with Reflection.Emit, and there seems to be a pre-existing distinction between signature and non-signature types. I don't mind either way personally.

  6. jkotas commented on Mar 28, 2026

    @jkotas
    Member

    What would it take to relax reflection APIs to make this and other similar scenarios work?

    there seems to be a pre-existing distinction between signature and non-signature types.

    The TypeBuilder-specific composite types predate introduction of signature types. I think about signature types as a more general replacement for the TypeBuilder-specific composite types.

  7. removed
    untriaged
    on Jul 24, 2026
  8. added this to the Future milestone on Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Reflection.Emit

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions