Skip to content

binding: find inherited static members through the derived class name - #55

Merged
rameel merged 2 commits into
mainfrom
binding/inherited-static-members
Nov 21, 2026
Merged

rameel merged 2 commits into
mainfrom
binding/inherited-static-members

Conversation

@rameel

@rameel rameel commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Static members of base classes were not found through the name of a derived class. With a static method S declared in BaseClass and DerivedClass registered with RegisterType, DerivedClass.S() failed with "Cannot resolve symbol 'S'", and so did string.ReferenceEquals(a, b). For interfaces, #50 already made this work.

This PR looks up static members through a type name with BindingFlags.FlattenHierarchy, as C# does. A field, property, or method hidden with new still resolves to the member of the derived class. The static methods of System.Enum are now available through an enum name, and string.Equals(1, 1) calls Object.Equals(object, object) and returns true instead of reporting an ambiguous match.

Unqualified names still use only the static members declared by the imported types: using static in C# does not import members of base classes either.

This relies on #54: without it, now that base class methods are visible, DerivedClass.M(1) would choose BaseClass.M(int) over DerivedClass.M(long).

Overload resolution chose the most specific of all applicable methods,
wherever they were declared. With BaseClass.M(int) and
DerivedClass.M(long), Cls.M(1) called BaseClass.M(int), and the same
happened with indexers and methods of base interfaces. C# calls
DerivedClass.M(long): it removes the applicable methods declared in a
base type of the type that declares another applicable method before
choosing the best one.

Apply this rule in MethodResolver. As in C#, an override belongs to
the type that introduced the method. This includes a covariant return
override, which reflection reports as a new method of the derived
class and which is chosen over the method it overrides. Base types are
found by inheritance rather than assignability, so instantiations of a
variant interface do not hide each other. A method of a class other
than Object also removes the methods of statically imported
interfaces.

The hierarchy depth no longer decides between methods with the same
parameters. The same method imported from two unrelated types is now
an ambiguous match, as in C#, instead of resolving to the type with
the deeper hierarchy.
Static members were looked up without BindingFlags.FlattenHierarchy,
so the static members of base classes were not found through the name
of a derived class: with S declared in BaseClass, DerivedClass.S()
failed with "Cannot resolve symbol 'S'", and so did
string.ReferenceEquals(a, b).

As in C#, a type name now gives access to the static members of its
base classes, including Object, and an enum name to those of
System.Enum. Members hidden with new still resolve to the derived
class. Unqualified names still use only the static members declared by
the imported types, as with using static in C#.
@rameel
rameel merged commit b3f5093 into main Nov 21, 2026
1 check passed
@rameel
rameel deleted the binding/inherited-static-members branch November 21, 2026 18: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.

1 participant