Skip to content

Add intrinsic analysis support for Type.GetMethod overload with genericParameterCount and BindingFlags #118003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 23, 2025

This PR adds missing intrinsic analysis support for the new Type.GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Type[] types) overload that was added in #94889.

Problem

The new overload was not properly handled by ILLink/ILC/ILLink.RoslynAnalyzer's intrinsic analysis. The current IntrinsicId.Type_GetMethod handling didn't account for the genericParameterCount parameter, causing the analysis to mark all methods with the given name regardless of their generic parameter count, rather than filtering by the specified count as the runtime does.

Solution

Enhanced the HandleCallAction.cs intrinsic handling to:

  1. Detect genericParameterCount parameter: Added logic to identify when GetMethod overloads include an int genericParameterCount parameter at index 2
  2. Extract genericParameterCount value: Added GetGenericParameterCountFromValue helper method to extract the parameter count from the argument values
  3. Filter by generic parameter count: Created new ProcessGetMethodByName overload that filters methods by their generic parameter count using GetGenericParameters().Length
  4. Handle all overload combinations: Supports both GetMethod(string, int, Type[]) and GetMethod(string, int, BindingFlags, Type[]) patterns

Testing

Added comprehensive test case GetMethod_Name_GenericParameterCount_BindingAttr_Types in MethodUsedViaReflection.cs to verify the analysis correctly marks only methods matching the specified generic parameter count.

The implementation ensures trimmer analysis behavior matches runtime behavior where:

// Only finds methods with exactly 1 generic parameter
var method = type.GetMethod("MyMethod", 1, BindingFlags.Public, new Type[] { typeof(int) });

Fixes #94989.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jul 23, 2025
@Copilot Copilot AI changed the title [WIP] Analysis support for new Type.GetMethod overload Add intrinsic analysis support for Type.GetMethod overload with genericParameterCount and BindingFlags Jul 24, 2025
@Copilot Copilot AI requested a review from sbomer July 24, 2025 00:13
Copilot finished work on behalf of sbomer July 24, 2025 00:13
[Kept]
public static void TestNameWithIntAndBindingFlagsAndTypes()
{
var method = typeof(GetMethod_Name_GenericParameterCount_BindingAttr_Types).GetMethod("OnlyCalledViaReflection", 0, BindingFlags.Public | BindingFlags.Static, new Type[] { typeof(int), typeof(int), typeof(int) });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test seems wrong; the OnlyCalledViaReflection methods must be generic because the integer and Type[] parameters in Type.GetMethod refer to generic parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linkable-framework Issues associated with delivering a linker friendly framework
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Analysis support for new Type.GetMethod overload
3 participants