Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

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:

interface IR1 {}
interface IRoot : IR1 { void Foo(int x); }
interface IBase : IRoot {}
interface IX { void Foo(); }
interface IDerived : IBase, IX {}

typeof(IDerived).TryFindMethod("Foo", BindingFlags.Public | BindingFlags.Instance, out MethodInfo? 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.

Activity

  1. added
    bug
    on Oct 31, 2026
  2. matt-edmondson commented on Nov 1, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • 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.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions