Skip to content

Conversation

@brunolins16
Copy link
Member

@brunolins16 brunolins16 commented Apr 22, 2022

Closes #40712

This PR will introduce a support for the [Parameters] attribute. The parameter will be created, based on the on the constructor parameters and/or writable properties, and the new expression will be similar to:

Parameterless constructor

arg_local = new T()
{
    arg_local.Property[0] = expression[0],
    arg_local.Property[n] = expression[n],
}

Where all writable properties will be initialized and included as endpoint parameter.

Parameterized constructor

arg_local = new T(arguments);

Where all constructor argument will be included as endpoint parameter. Also, in this case the arguments attributes will be used as primary source for attributes and fallback to the matching property when nothing defined.

Fo all parameters detected the same Bind rules, currently available, will be applied.

Constructor detection mechanism

I have tried to match as much as possible the rules described in spec but some behavior diverges from it. With that, here is how the [Parameter] type constructor is detected.

class

  • A public parameterless constructor will be used if present
  • A public parameterized constructor will be use if a single constructor is present and all arguments has a matching (case insensitive) public property.
    • If a constructor parameter does not match with a property, InvalidOperationException will be thrown if deserialization is attempted.
  • Throw InvalidOperationException when more than one parameterized is declared and the parameterless constructor is not present.
  • Throw InvalidOperationException if not able to find a correct constructor.

struct

  • A declared public parameterless constructor will always be used if present
  • A public parameterized constructor will be use if a single constructor is present and all arguments has a matching (case insensitive) public property.
    • If a constructor parameter does not match with a property, InvalidOperationException will be thrown if deserialization is attempted.
  • Since struct always has a default constructor, the default constructor will be used if it is the only one present or more than one parameterized constructor is present.

The same rules applied for records.

Examples

Valid

public [class or struct] Bar
{
    public int Foo { get; set; }
}

public [class or struct] Bar
{
    public Bar()
    {}

    public int Foo { get; set; }
}

public [class or struct] Bar
{
    public Bar(int foo)
    { }

    public int Foo { get; }
}


public [class or struct] Bar
{
    public Bar()
    {}

    public Bar(int foo)
    {}

    public int Foo { get; set; }
}

Invalid

public  [class or struct] Bad
{
    public Bad(int foo)
    { }
    public Bad(int foo, int bar)
    {}
    public int Foo { get; set; }
    public int Bar { get; set; }
}

public  [class or struct] Bad
{
    public Bad(int foo, string otherArgument)
    { }
    public int Foo { get; set; }
}

Benchmark

Results here: https://gist.github.com/brunolins16/901f351f4db7b14e69e1c58677922dd0

@ghost ghost added the area-runtime label Apr 22, 2022
@brunolins16 brunolins16 linked an issue Apr 22, 2022 that may be closed by this pull request
1 task
@brunolins16 brunolins16 added old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-runtime labels Apr 25, 2022
Copy link
Member

@BrennanConroy BrennanConroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens with multiple [AsParameters] like

void TestMethod([AsParameters] One, [AsParameters] Two) { }

Should a test be added for it?

@brunolins16
Copy link
Member Author

What happens with multiple [AsParameters] like

void TestMethod([AsParameters] One, [AsParameters] Two) { }

Should a test be added for it?

It will work but I think I missed a test for it. Let me add it.

@halter73
Copy link
Member

Really good job on this. The PR looks super clean.

Copy link
Member

@davidfowl davidfowl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OutkastSoFreshSoCleanGIF

@brunolins16 brunolins16 enabled auto-merge (squash) May 12, 2022 20:58
@brunolins16 brunolins16 merged commit 65bb1ec into dotnet:main May 12, 2022
@ghost ghost added this to the 7.0-preview5 milestone May 12, 2022
@brunolins16 brunolins16 deleted the brunolins16/issues/40712 branch August 2, 2022 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for argument list surrogates in minimal APIs

5 participants