Skip to content

NativeAOT MakeGenericType fails to activate reflected generic type when generic parameter has self-referential interface constraint #126604

Description

@vyacheslav-volkov

Description

NativeAOT throws at runtime when creating a closed generic type via:

  • typeof(GenericType<,>).MakeGenericType(...)
  • Activator.CreateInstance(...)

if the generic type parameter has this constraint:

where TRequest : struct, IRequest<TRequest, TResponse>

The same code works if the self-referential interface constraint is removed and replaced with just:

where TRequest : struct

This looks incorrect, because the closed generic instantiation is valid and used at runtime with compatible type arguments, but NativeAOT does not preserve the required metadata/native code.

Reproduction Steps

public interface IGenericType<T, TResponse> where T : allows ref struct
{
    void Invoke(T value);
}

public interface IRequest<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TRequest, TResponse>
    where TRequest : IRequest<TRequest, TResponse>, allows ref struct;

public struct TestRequest : IRequest<TestRequest, string>;

public class GenericType<TRequest, TResponse> : IGenericType<TRequest, TResponse> where TRequest : struct, IRequest<TRequest, TResponse>
{
    public void Invoke(TRequest value) => Console.WriteLine($"value:{value}");
}

public class TypeHandler
{
    public void Handle<TRequest, TResponse>() where TRequest : allows ref struct
    {
        if (typeof(TRequest).IsValueType)
        {
            var genericType = (IGenericType<TRequest, TResponse>) Activator.CreateInstance(typeof(GenericType<,>).MakeGenericType(typeof(TRequest), typeof(TResponse)))!;
            genericType.Invoke(default!);
        }
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        new TypeHandler().Handle<TestRequest, string>();
    }
}
  1. Publish the repro with NativeAOT enabled.
  2. Run the produced executable.

Expected behavior

NativeAOT should handle constraints correctly in this case since it generates metadata if I remove the constraint.

Actual behavior

Runtime throws:
Unhandled exception. System.NotSupportedException: '...GenericType`2[...TestRequest,System.String]' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Apr 7, 2026
  2. dotnet-policy-service commented on Apr 7, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
    See info in area-owners.md if you want to be subscribed.

  3. added a commit that references this issue on Apr 21, 2026
    7bba220
  4. removed
    untriaged
    on Apr 21, 2026
  5. MichalStrehovsky commented on Apr 22, 2026

    @MichalStrehovsky
    Member

    The fix is getting revered, reopen.

  6. added
    untriaged
    on Apr 22, 2026
  7. added this to the 11.0.0 milestone on Jun 16, 2026
  8. removed
    untriaged
    on Jun 16, 2026
  9. added a commit that references this issue on Jun 18, 2026
    ee2a8a9
  10. added 2 commits that reference this issue on Jul 15, 2026
    82036d3
    5acf980
  11. locked and limited conversation to collaborators on Jul 19, 2026
  12. added a commit that references this issue on Jul 22, 2026
    88fe5fc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions