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
[API Proposal]: Add API to support emitting constructed generic types from varying reflection stacks. #126182
#125612 exposed a feature gap in PersistedAssemblyBuilder when used alongside MetadataLoadContextas 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. TypeBuilderalready 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);
}
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.
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.
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.
Activity
dotnet-policy-service commentedon Mar 27, 2026
Tagging subscribers to this area: @dotnet/area-system-reflection-emit
See info in area-owners.md if you want to be subscribed.
jkotas commentedon Mar 27, 2026
Why is
Type.MakeGenericSignatureTypenot sufficient?teo-tsirpanis commentedon Mar 27, 2026
The linked issue's repro did use
MakeGenericSignatureType, but failed with:We could fix this by making more APIs work on signature types and relaxing some Reflection.Emit APIs to work with
Typeinstances of any origin, but people are already usingMakeGenericTypewith Reflection.Emit, and there seems to be a pre-existing distinction between signature and non-signature types. I don't mind either way personally.jkotas commentedon Mar 28, 2026
What would it take to relax reflection APIs to make this and other similar scenarios work?
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.