Skip to content

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

@JamesNK

Description

@JamesNK

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CompilersuntriagedIssues and PRs which have not yet been triaged by a lead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions