Skip to content

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

Closed

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

Activity

  1. ghost added
    untriaged
    on Mar 5, 2022
  2. steveharter commented on Mar 9, 2022

    @steveharter
    Contributor

    This does appear to be a bug; the binding mechanism is supposed to account for parameter names and default values. Even if not supported, IndexOutOfRangeException should not be thrown.

  3. added and removed
    untriaged
    on Mar 9, 2022
  4. self-assigned this
    on Mar 9, 2022
  5. added this to the 7.0.0 milestone on Mar 9, 2022
  6. self-assigned this
    on Jun 17, 2022
  7. ghost added
    in-pr
    on Jun 20, 2022
  8. ghost removed
    in-pr
    on Jul 6, 2022
  9. ghost locked as resolved and limited conversation to collaborators on Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions