You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TryFindMethod on an interface returns a grandparent interface's method over a direct parent's, because levels are ordered by ancestor count, not distance #178
The remarks on TryFindMethod (Extensions/ReflectionExtensions.cs:17-19) promise that an interface's inherited interfaces are "searched after it, more-derived interfaces first". SearchLevels (:67-85) actually groups type.GetInterfaces() by how many interfaces each one itself inherits (GroupBy(i => i.GetInterfaces().Length)), and searches the largest group first. That measures how deep an interface's own ancestry runs, not how far it sits from type. A direct parent with no bases therefore ends up in the last group, behind interfaces that are further away.
Repro
Run on main, built for net10.0:
interfaceIR1{}interfaceIRoot:IR1{voidFoo(intx);}interfaceIBase:IRoot{}interfaceIX{voidFoo();}interfaceIDerived:IBase,IX{}typeof(IDerived).TryFindMethod("Foo",BindingFlags.Public|BindingFlags.Instance,outMethodInfo?mi);// mi == IRoot.Foo(int) — IRoot is two levels up; IX is a direct parent and declares Foo()
The groups also mix distances: IX (distance 1) and IR1 (distance 3) both land in the "0 ancestors" group, so the fewest-parameters tie-break can pick between interfaces at different distances.
Why it matters
The result depends on how many bases unrelated interfaces happen to have. Adding a base interface to IX silently changes which method TryFindMethod returns. For classes, the walk stops at the nearest level that declares the name. For interfaces, it doesn't.
Suggested fix / acceptance criteria
Build the levels breadth-first by inheritance distance:
Level 1 is type's direct interfaces: the members of type.GetInterfaces() that aren't inherited by another member of that set.
Each later level is the direct bases of the previous level that haven't been visited yet.
Add the scenario above as a test, expecting IX.Foo(), plus a test showing that adding a base to IX doesn't change the result.
Category: Bug (reflection lookup order contradicts the documented contract)
Priority: Medium. TryFindMethod can silently return the wrong overload for interface hierarchies, and the result shifts when an unrelated interface gains a base. It needs diamond-ish interface graphs with overloads at different distances, so real-world exposure is limited.
Suggested assignment: Extensions maintainer, area ReflectionExtensions.
Duplicates / related: No duplicate found in Extensions or elsewhere in the org.
In progress? No open PR covers this.
Next step: Replace the GroupBy(i => i.GetInterfaces().Length) levels with a breadth-first walk by inheritance distance. Then add the IDerived test, which expects IX.Foo(), and the stability test that adds a base to IX.
Activity
matt-edmondson commentedon Nov 1, 2026
Triage
TryFindMethodcan silently return the wrong overload for interface hierarchies, and the result shifts when an unrelated interface gains a base. It needs diamond-ish interface graphs with overloads at different distances, so real-world exposure is limited.ReflectionExtensions.GroupBy(i => i.GetInterfaces().Length)levels with a breadth-first walk by inheritance distance. Then add theIDerivedtest, which expectsIX.Foo(), and the stability test that adds a base toIX.Generated by Claude Code