Skip to content

Optional ParameterInfo is lost at runtime for local functions #51518

@halter73

Description

@halter73

Version Used:

master (24 Feb 2021) on sharplab.io

Steps to Reproduce:

View this sharplab.io snippet and observe that this:

using System;
public class C {
    public void M() {
        void TestLocalAction(int a = 2)
        {
        }
        
        MapAction((Action<int>)TestMethodAction);
        MapAction((Action<int>)TestLocalAction);
    }
    
    public void TestMethodAction(int a = 2)
    {
    }
    
    public void MapAction(Delegate action)
    {
    }
}

results in this:

// ...
public class C
{
    public void M()
    {
        MapAction(new Action<int>(TestMethodAction));
        MapAction(new Action<int>(<M>g__TestLocalAction|0_0));
    }

    public void TestMethodAction(int a = 2)
    {
    }

    public void MapAction(Delegate action)
    {
    }

    [CompilerGenerated]
    internal static void <M>g__TestLocalAction|0_0(int a)
    {
    }
}

And that <M>g__TestLocalAction|0_0 does not have a default value unlike TestMethodAction.

Expected Behavior:

<M>g__TestLocalAction|0_0 should have int a = 2 in its parameter list.

Actual Behavior:

<M>g__TestLocalAction|0_0 does not have int a = 2 in its parameter lists. This becomes a problem for MapAction if it wants to call TestLocalAction with the default parameter value. Delegate.Method.GetParameters()[0].HasDefaultValue ends up returning false instead of true.

Thanks @Kahbazi for finding this! https://github.com/dotnet/aspnetcore/pull/30434/files#r583502436

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions