Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Tracked by #44655
calbonaler opened this issue Mar 5, 2022 · 3 comments · Fixed by #71013
Closed
Tracked by #44655

Comments

@calbonaler
Copy link

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Mar 5, 2022
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost
Copy link

ghost commented Mar 5, 2022

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: calbonaler
Assignees: -
Labels:

area-System.Reflection, untriaged

Milestone: -

@steveharter
Copy link
Member

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.

@steveharter steveharter added bug and removed untriaged New issue has not been triaged by the area owner labels Mar 9, 2022
@steveharter steveharter self-assigned this Mar 9, 2022
@steveharter steveharter added this to the 7.0.0 milestone Mar 9, 2022
@buyaa-n buyaa-n self-assigned this Jun 17, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 20, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 6, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants