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

Support for init properties in dynamic binder #40118

Closed
HaloFour opened this issue Jul 30, 2020 · 5 comments · Fixed by #41293
Closed

Support for init properties in dynamic binder #40118

HaloFour opened this issue Jul 30, 2020 · 5 comments · Fixed by #41293
Assignees
Milestone

Comments

@HaloFour
Copy link

See: dotnet/csharplang#39 (comment)

Seems that the C# runtime binder ignores modreq metadata on members. This enables init properties to be trivially mutated by casting to dynamic:

public class Foo {
    public string Value { get; init; }
}

static class Program {
    static void Main() {
        var foo = new Foo { Value = "Hello" };
        ((dynamic)foo).Value = "World";
        Console.WriteLine(foo.Value); // prints World
    }
}
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jul 30, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

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.

@mangod9
Copy link
Member

mangod9 commented Jul 30, 2020

@HaloFour I assume you expect this to not compile in c#, additionally the runtime to also throw?

@HaloFour
Copy link
Author

@mangod9

I'd expect it to compile in C# (since it's being cast to dynamic) but to throw at runtime. It probably should be doing this already since the member would have a modreq applied to it and shouldn't be invoked without the binder understanding what that modifier entails.

If you take dynamic out of the equation and attempt to mutate the property it will fail to compile: SharpLab

@ghost
Copy link

ghost commented Jul 30, 2020

Tagging subscribers to this area: @cston
See info in area-owners.md if you want to be subscribed.

@HaloFour
Copy link
Author

@cston cston removed the untriaged New issue has not been triaged by the area owner label Aug 14, 2020
@cston cston added this to the 5.0.0 milestone Aug 14, 2020
@cston cston self-assigned this Aug 14, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
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.

5 participants