Skip to content

[NativeAOT] Support invoking abstract constructors on existing instances through MethodInvoker #134903

Description

@jkoritzinsky

Description

The reflection invocation tests for abstract constructors invoked on an existing instance are currently limited to CoreCLR because NativeAOT does not support the behavior exercised by MethodInvokerTests.Constructor_AbstractDeclaringTypeWithExistingInstance, Constructor_AbstractDeclaringTypeWithExistingInstance_RegularArguments, and Constructor_AbstractDeclaringTypeWithExistingInstance_ManyRegularArguments.

NativeAOT should support invoking an instance constructor declared on an abstract base type against an existing compatible derived object, including regular and by-ref argument paths.

Reproduction Steps

Run the System.Reflection.Tests tests listed above on NativeAOT. The tests are currently marked ConditionalFact(IsCoreCLR) and should become active on NativeAOT once the behavior is implemented.

Representative pattern:

ConstructorInfo constructor = typeof(AbstractRegularConstructorTarget).GetConstructor(
    BindingFlags.Instance | BindingFlags.NonPublic,
    binder: null,
    new[] { typeof(int) },
    modifiers: null)!;
MethodInvoker invoker = MethodInvoker.Create(constructor);
var target = new ConcreteRegularConstructorTarget(0);
invoker.Invoke(target, 42);

Expected behavior

The constructor runs against the existing derived instance and updates its state, as it does on CoreCLR. Allocating an instance for an abstract declaring type and invoking with a null target should remain rejected.

Actual behavior

The behavior is not currently supported on NativeAOT, so these tests cannot run there and are guarded to CoreCLR.

Regression?

Unknown.

Known Workarounds

None.

Configuration

NativeAOT runtime; System.Reflection.Tests.

Other information

Relevant tests: src/libraries/System.Runtime/tests/System.Reflection.Tests/MethodInvokerTests.cs.

[!NOTE] This issue was filed with assistance from GitHub Copilot.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions