Fix NativeAOT delegate commands by preserving reflection metadata #203
+229
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before this PR:
When using delegate handlers such as:
and
Commands.Rootincluded attributes likeValidationAttributes, the generated code would still callcommand.Method.GetParameters()even after source generation, This requires metadata that may be trimmed when compiling with Native AOT, causing a runtime exception.What this PR does:
MethodInfowas trimmed away - exit code != 0 and test fails.Emitterto fix the issueSymbolDisplayFormatto fully qualify type names.command.CommandMethodInfois null, meaning the runtime will rely on reflection to discover the delegate’s parameters, the emitter now uses the handler’sIMethodSymbolto create aDynamicDependencyattribute that points to the handler’s containing type.RunCommandmethod signature.CommandMethodInfois not null, no changes are made.Rationale:
This change ensures that the necessary
MethodInfois preserved when it would otherwise be trimmed.An alternative approach would be to cache the method and its parameters during build time, but that would require significant refactoring of
Emitter. The current solution fixes the issue for affected users, and caching could be explored as a potential improvement in the future.