Skip to content

Honour BindingFlags.IgnoreCase in TryFindMethod [patch] - #196

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/193-trufindmethod-ignorecase
Nov 10, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/193-trufindmethod-ignorecase

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #193

What was wrong

TryFindMethod collects candidates with GetMethods(bindingFlags | DeclaredOnly), which doesn't filter by name, and then filtered them with StringComparison.Ordinal whatever the flags said. A caller's BindingFlags.IgnoreCase was silently dropped, so typeof(string).TryFindMethod("toupperinvariant", Public | Instance | IgnoreCase, out _) returned false. This regressed with the #155 overload fix: the method used to call GetMethod(name, flags), which honours IgnoreCase.

What changed

  • The name comparison is OrdinalIgnoreCase when IgnoreCase is set, and Ordinal otherwise.
  • A case-insensitive search ranks an exact-case match first, ahead of the existing parameter-count, generic, and signature ordering. When two methods differ only by case, the result is therefore deterministic and follows the spelling the caller used.
  • The XML doc remarks describe both rules.
  • Five tests are new:
    • IgnoreCase on a class (string.ToUpperInvariant), checked against what GetMethod returns
    • IgnoreCase on a base-class member
    • IgnoreCase on an interface ancestor (IList<int> → ICollection<int>.Add)
    • a lookup without IgnoreCase stays case-sensitive
    • with Run() and run(int) on one class, "run" and "Run" each pick their exact-case method, and "RUN" falls back to fewest parameters

Verification

  • With the source change reverted, 4 of the new tests fail. The case-sensitive one passes, as it should.
  • The full suite passes on net10.0: 240 of 240.

Generated by Claude Code

TryFindMethod lists candidates with GetMethods, which does not filter by
name, then compared names ordinally regardless of the flags, so IgnoreCase
was silently dropped (a regression from the #155 overload fix, which
replaced GetMethod). The comparison now follows the flags, and a
case-insensitive search prefers an exact-case match before the existing
parameter-count, generic and signature ordering.

Fixes #193

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3FDxy5QE2xUSYyNqC
@sonarqubecloud

sonarqubecloud Bot commented Nov 8, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 65d1a59 into main Nov 10, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/193-trufindmethod-ignorecase branch November 10, 2026 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TryFindMethod ignores BindingFlags.IgnoreCase: typeof(string).TryFindMethod("toupperinvariant", … | IgnoreCase) returns false (regression from #155)

2 participants