[release/10.0] Fix Native AOT-incompatible reflection in CalendarTestBase#126817
[release/10.0] Fix Native AOT-incompatible reflection in CalendarTestBase#126817Copilot wants to merge 2 commits intorelease/10.0from
Conversation
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/a684b399-ef09-407d-8de5-9e4680d4f736 Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-globalization |
|
/azp run runtime-nativeaot-outerloop |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
This PR aims to restore NativeAOT test compatibility by adjusting reflection usage in the Japanese calendar test infrastructure (CalendarTestBase) on release/10.0, after a prior change introduced trimming/AOT-incompatible reflection patterns.
Changes:
- Reworks reflection used to compute the ICU-specific
JapaneseCalendarminimum supported date (era ticks) to be more NativeAOT-friendly.
| object[] eraInfo = (object[])typeof(JapaneseCalendar).GetMethod("GetEraInfo", BindingFlags.NonPublic | BindingFlags.Static)!.Invoke(null, null)!; | ||
| Type eraInfoType = Type.GetType("System.Globalization.EraInfo, System.Private.CoreLib")!; |
There was a problem hiding this comment.
Type.GetType(string) is annotated with RequiresUnreferencedCode on CoreCLR, so this line will produce trimming/AOT analyzer warnings (and likely fail NativeAOT test builds). Consider avoiding Type.GetType(...) here; you can obtain the EraInfo element type via the reflected GetEraInfo method signature (e.g., from MethodInfo.ReturnType.GetElementType()), which avoids string-based type lookup and the RUC annotation.
| object[] eraInfo = (object[])typeof(JapaneseCalendar).GetMethod("GetEraInfo", BindingFlags.NonPublic | BindingFlags.Static)!.Invoke(null, null)!; | |
| Type eraInfoType = Type.GetType("System.Globalization.EraInfo, System.Private.CoreLib")!; | |
| MethodInfo getEraInfoMethod = typeof(JapaneseCalendar).GetMethod("GetEraInfo", BindingFlags.NonPublic | BindingFlags.Static)!; | |
| object[] eraInfo = (object[])getEraInfoMethod.Invoke(null, null)!; | |
| Type eraInfoType = getEraInfoMethod.ReturnType.GetElementType()!; |
Change #122373 broke native AOT testing because it introduced trimming incompatible code into a test.
/cc @MichalStrehovsky
Customer Impact
Test only change. We broke release/10.0 using a release/10.0-only change.
Regression
Testing
Outerloop run.
Risk
Low. Test only.
IMPORTANT: If this backport is for a servicing release, please verify that:
release/X.0-staging, notrelease/X.0.release/X.0(no-stagingsuffix).Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.