Skip to content

TryFindMethod returns false for methods an interface inherits from its base interfaces: typeof(IList<int>).TryFindMethod("Add", …) finds nothing #158

Closed

Description

@matt-edmondson

What's wrong

ReflectionExtensions.TryFindMethod (Extensions/ReflectionExtensions.cs:30-38) climbs the type hierarchy through Type.BaseType. An interface type's BaseType is always null, and Type.GetMethod on an interface does not search the interfaces it extends. So when type is an interface, the search covers only the interface's own declared members. Every method inherited from a base interface is reported as missing, even though the doc comment says the method "walks up the inheritance tree".

Reproduction (run against a local build, net10.0)

using System.Reflection;
using ktsu.Extensions;

public interface IBase { void BaseOp(); }
public interface IDerived : IBase { }

var flags = BindingFlags.Public | BindingFlags.Instance;
typeof(IDerived).TryFindMethod("BaseOp", flags, out var m1);          // false, m1 == null
typeof(IList<int>).TryFindMethod("Add", flags, out var m2);           // false (Add is declared on ICollection<T>)
typeof(IList<int>).TryFindMethod("GetEnumerator", flags, out var m3); // false (declared on IEnumerable<T>)
typeof(ICollection<int>).TryFindMethod("Add", flags, out var m4);     // true

Observed output:

IList`1.GetEnumerator: False
ICollection`1.Add: True ICollection`1
IList`1.Add: False
IDerived.BaseOp: False

Expected: true in all four cases, with methodInfo pointing at the method on the interface that declares it.

Why it matters

Code that discovers members by convention is often handed an interface type, such as a service contract or typeof(IList<T>). For those types the method returns false for members that do exist. Because this is a Try method, the caller reads false as "not present" and silently skips the member or takes a fallback path, with no error to point at the cause. The existing tests in ReflectionExtensionsTests.cs cover class inheritance only.

Suggested fix / acceptance criteria

Activity

  1. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Suggested next step: When type.IsInterface, fall back to searching type.GetInterfaces(). Add a two-level interface hierarchy test plus the IList<int> cases from the issue.


    Generated by Claude Code

  2. added a commit that references this issue on Oct 14, 2026
    19782f2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions