You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot set generic parent for TypeBuilder using System.Reflection.Metadata.MetadataLoadContext and Reflection.Emit #125612
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.
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.
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:
Emit the data‑model assembly dynamically.
Let users reference that assembly in their own project.
Compile their project.
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?
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.
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.
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.
Activity
dotnet-policy-service commentedon Mar 16, 2026
Tagging subscribers to this area: @dotnet/area-system-reflection-emit
See info in area-owners.md if you want to be subscribed.
alexander-clearis commentedon Mar 19, 2026
I originally posted this issue regarding a failure when mixing
System.Reflection.EmitwithSystem.Reflection.Metadata.MetadataLoadContextin .NET 10.0. The core problem is that combining a dynamic type with a generic runtime type loaded through theMetadataLoadContextcauses failures.Additional Problems I’m Now Seeing
After further experimentation, I’m no longer sure that using
MetadataLoadContexttogether withReflection.Emitis viable at all for my scenario. More issues appear when mixing emitted types with metadata-loaded types, like callingMethodInfo.MakeGenericMethodfails when:MetadataLoadContext,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:
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:
This happens even though the external project already references the standard .NET assemblies. The mismatch between
System.Runtime(used by normal projects) andSystem.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:
teo-tsirpanis commentedon Mar 27, 2026
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 inGet***methods, and most of their APIs throw.However, types from a
MetadataLoadContextdo not supportMakeGenericTypewith 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 foreignTypeinstances 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.MakeGenericTypeimplementation. This will require new APIs, for which I will open a separate issue.steveisok commentedon Aug 3, 2026
@teo-tsirpanis is this something you still want to take on?
teo-tsirpanis commentedon Aug 3, 2026
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.