Skip to content

Cannot set generic parent for TypeBuilder using System.Reflection.Metadata.MetadataLoadContext and Reflection.Emit #125612

Description

@alexander-clearis

Description

In .NET 10.0, combining System.Reflection.Emit with System.Reflection.Metadata.MetadataLoadContext fails for dynamic type emission involving generics. Specifically, constructing a closed generic type (e.g., StateMachine<TestState>) from metadata and setting it as a parent for a TypeBuilder triggers unsupported operations on SignatureType, such as checking Type.IsInterface.

Reproduction Steps

This issue can be reproduced using the minimal project available at https://github.com/alexander-clearis/EmitGenericMixedContext.git

Project Explanation:
The repository contains a simple .NET 10.0 solution with two projects:

  • Common: Defines basic types, including a generic interface IStateMachine<TValue> (empty for repro purposes) and related helpers.

Emitter: The main project that demonstrates the bug. It uses MetadataLoadContext (with a custom resolver for core assemblies) to load generics, emits new types via Reflection.Emit, and attempts to inherit from a constructed generic (StateMachine<TestState>). The entry point is Program.cs, which generates and saves a dynamic assembly ("TestAssembly.dll").

Expected behavior

SetParent and CreateType succeed, allowing the new type to inherit from the metadata-loaded generic without exceptions.

Actual behavior

NotSupportedException is thrown due to limitations in SignatureType reflection support.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Mar 16, 2026
  2. dotnet-policy-service commented on Mar 16, 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.

  3. alexander-clearis commented on Mar 19, 2026

    @alexander-clearis
    Author

    I originally posted this issue regarding a failure when mixing System.Reflection.Emit with System.Reflection.Metadata.MetadataLoadContext in .NET 10.0. The core problem is that combining a dynamic type with a generic runtime type loaded through the MetadataLoadContext causes failures.

    Additional Problems I’m Now Seeing

    After further experimentation, I’m no longer sure that using MetadataLoadContext together with Reflection.Emit is viable at all for my scenario. More issues appear when mixing emitted types with metadata-loaded types, like calling MethodInfo.MakeGenericMethod fails when:

    • the method belongs to a type loaded via MetadataLoadContext,
    • the generic argument is a type emitted via Reflection.Emit.

    Why I Was Trying This

    I already have a data‑model generator that generates types at runtime. Now I want to write unit tests for that model and allow users to write custom hooks in external C# projects. The idea was:

    1. Emit the data‑model assembly dynamically.
    2. Let users reference that assembly in their own project.
    3. Compile their project.
    4. Load their compiled output back into my application alongside the emitted assembly.

    Assemblies produced by Reflection.Emit always reference System.Private.CoreLib, not System.Runtime.
    This becomes a problem when I try to reference the emitted DLL from an external project, then the compiler reports errors such as:

    “The type 'Nullable<>' is required here and is unavailable. You must add a reference to assembly 'System.Private.CoreLib'.”

    This happens even though the external project already references the standard .NET assemblies. The mismatch between System.Runtime (used by normal projects) and System.Private.CoreLib (used by emitted assemblies) causes the build to fail.

    To help clarify the intended behavior and determine whether my scenario is supported, I have a few follow‑up questions:

    • Is mixing emitted runtime types with types loaded through MetadataLoadContext expected to work at all, or is this an unsupported scenario by design?
    • If this is a bug, is there a recommended workaround or alternative API for constructing mixed generic types?
    • Is there a supported way to emit assemblies that reference System.Runtime instead of System.Private.CoreLib, so that they behave like normal SDK‑style projects?
    • If not, is there guidance on how external projects are supposed to reference dynamically emitted assemblies without running into missing core type definitions?
  4. teo-tsirpanis commented on Mar 27, 2026

    @teo-tsirpanis
    Contributor

    I took a look at this. The first thing I tried was to change this line to stateMlcType.MakeGenericType(stateTypeBuilder), because signature types are used for overload matching in Get*** methods, and most of their APIs throw.

    However, types from a MetadataLoadContext do not support MakeGenericType with types not from the same MLC. While this limitation is understandable given MLC's goal of providing a closed type universe, this behavior is contrary to runtime reflection and Reflection.Emit, both of which supporting generic instantiations with foreign Type instances as parameters.

    The best way to resolve this IMO would be to relax MLC's "closed universe"ness and support foreign type parameters in its Type.MakeGenericType implementation. This will require new APIs, for which I will open a separate issue.

  5. removed
    untriaged
    on Jul 23, 2026
  6. steveisok commented on Aug 3, 2026

    @steveisok
    Member

    @teo-tsirpanis is this something you still want to take on?

  7. teo-tsirpanis commented on Aug 3, 2026

    @teo-tsirpanis
    Contributor

    I did some work last month towards the suggestion in #126182 (comment) to make signature types more usable. I don't remember off my head why I had stopped, but I can continue working on it.

  8. added this to the 12.0.0 milestone on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions