Skip to content

Method binding by name with Type.DefaultBinder.BindToMethod fails with specific condition #66237

Closed
@calbonaler

Description

@calbonaler

Description

Type.DefaultBinder.BindToMethod throws IndexOutOfRangeException with the following conditions.

  • All the arguments are specified by name.
  • Some methods to bind have any unspecified parameters before the specified one.
  • All the unspecified parameters have default values.

Reproduction Steps

  1. Define the following class and method.
public static class Sample
{
	public static void SampleMethod(string param1, int param2 = 0, bool param3 = false) { }
}
  1. Bind to the above method with specifying parameter param1 and param3.
var methods = typeof(Sample).GetMethods();
var methodArgs = new object?[] { "value", true };
var method = Type.DefaultBinder.BindToMethod(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, methods, ref methodArgs, null, null, new[] { "param1", "param3" }, out var _);
  1. IndexOutOfRangeException is thrown at the last line of the above code.

Expected behavior

For example, C# compiler can compile the following code.

Sample.SampleMethod(param1: "value", param3: true);

So I expect that the binding succeeds and no exceptions are thrown.

Actual behavior

IndexOutOfRangeException is thrown and the binding fails.

Regression?

I reproduced this issue on the following platforms:

  • .NET Core 3.1
  • .NET Framework 4.8

Known Workarounds

No response

Configuration

  • Runtime: .NET 6.0
  • OS: Windows 10 (x64) Home 21H1

I don't think this issue is specific to the configuration.

Other information

No response

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions