dotnet / dotnet/dotnet-api-docs

Type.GetMethod overloads accepting a Type array to match arguments will allow parameter base classes / interfaces

Open
#9,090 0 comments 0 reactions 0 assignees View on GitHub
area-System.Reflection doc-bug Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

The docs describe the `types` parameter as follows:
> An array of [Type](https://learn.microsoft.com/en-us/dotnet/api/system.type?view=net-7.0) objects representing the number, order, and type of the parameters for the method to get.

This implies that the parameter types should match exactly, but that is not the case. For example, given:
```cs
class MyClass { public void Foo (MyClass obj) { } }
class MySubClass : MyClass { }
```

The call `typeof(MyClass).GetMethod("Foo", new[] { typeof(MySubClass) })` will actually match `Foo (MyClass obj)`.
It looks like the behavior is more like "match the most specific method **that can be invoked** with the provided parameters".
So in the above case, since `Foo (MyClass obj)` can be called with a `MySubClass` parameter, it will also be matched.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.