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

Local methods and delegates aren't populating ParameterInfo.DefaultValue #53478

Closed
JamesNK opened this issue May 18, 2021 · 5 comments · Fixed by #53402
Closed

Local methods and delegates aren't populating ParameterInfo.DefaultValue #53478

JamesNK opened this issue May 18, 2021 · 5 comments · Fixed by #53402
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@JamesNK
Copy link
Member

JamesNK commented May 18, 2021

Description

ParameterInfo.DefaultValue isn't available with local methods (and I'm guessing delegates as well). In this ASP.NET Core feature we want to use the default value information to make decisions about how a delegate will be called. e.g. use the default value when calling the method if no other value is available.

[Fact]
public void DefaultValueTest()
{
    void TestAction([FromRoute] int foo1 = 5)
    {
    }

    var method = GetType().GetMethod(nameof(TestAction), BindingFlags.Instance | BindingFlags.NonPublic)!;
    var p1 = method.GetParameters();
    Console.WriteLine(p1[0].HasDefaultValue);
    Console.WriteLine(p1[0].DefaultValue);
    // true
    // 5

    var d = (Action<int>)TestAction;
    var p2 = d.Method.GetParameters();
    Console.WriteLine(p2[0].HasDefaultValue);
    Console.WriteLine(p2[0].DefaultValue);
    // false
    // DBNull.Value
}

private void TestAction([FromRoute] int foo1 = 5)
{
}

Configuration

Host (useful for support):
Version: 6.0.0-preview.2.21154.6
Commit: 3eaf1f316b

Regression?

No

@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.

@JamesNK
Copy link
Member Author

JamesNK commented May 18, 2021

@halter73 @davidfowl

@jkotas jkotas transferred this issue from dotnet/runtime May 18, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 18, 2021
@jkotas
Copy link
Member

jkotas commented May 18, 2021

Roslyn does not emit the default values, so reflection does not return them.

@cston
Copy link
Member

cston commented May 18, 2021

Thanks @JamesNK.

This looks like a duplicate of #51518 which is being addressed in #53402.

Youssef1313 added a commit to Youssef1313/roslyn that referenced this issue May 18, 2021
@Youssef1313
Copy link
Member

Indeed. I added a test for this issue in #53402

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants