Cannot set generic parent for TypeBuilder using System.Reflection.Metadata.MetadataLoadContext and Reflection.Emit
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Description
### 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`) 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](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 `(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`). 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_
Contributor guide
Assessment
This issue has not been assessed yet.