Prevent visual studio from limiting the setter method to internal
Asked Answered
T

3

26

Well, I use visual studio 2015 CE, update 2. One productivity hack I usually do is that I create empty model classes like:

public class PersonModel
{
}

and then use them in a select expression like:

db.People.Where(p => someCondition)
.Select(p => new PersonModel
{
    Id = p.Id,
    Name = p.Name,
    //set other properties
}).ToList();

Then I go to the yet non-existing properties Id and Name, ... and press Control+. to ask visual studio to generate property Id for me. All great, but it will create:

public int Id { get; internal set; }

and if I use the same method in an asp.net webapi model binding, the binding will fail silently and give me Id = 0.

So my question is: is there any option to ask VS to create public setter, i.e.:

public int Id { get; set; }
Toor answered 26/6, 2016 at 10:30 Comment(2)
2021 and Visual Studio's incessant and inflexible use of the internal keyword in code generation is still knee-capping the potential productivity increases.Echeverria
grrrr, this needs sorting, so annoying. Does anyone know if there is an open user voice to upvote it?Purr
H
1

So my question is: is there any option to ask VS to create public setter, i.e.:

No, there is no way to modify the shortcut to automatically add the property with a public set. Although your productivity hack is neat, your best bet is to create it yourself.

Howell answered 9/1, 2019 at 19:50 Comment(1)
This is horrid they don't give you any way to change that autocomplete.Staggers
O
1

You can do that by typing prop and press the TAB twice. It will require you to enter type and name for property though, won't extract it from existing one.

Best regards.

Ohara answered 1/6, 2018 at 14:49 Comment(0)
H
1

So my question is: is there any option to ask VS to create public setter, i.e.:

No, there is no way to modify the shortcut to automatically add the property with a public set. Although your productivity hack is neat, your best bet is to create it yourself.

Howell answered 9/1, 2019 at 19:50 Comment(1)
This is horrid they don't give you any way to change that autocomplete.Staggers
Z
1

Still the same in Visual Studio 2017 and I use the same "productivity hack".

I recently got Resharper and it does use the public setter as we would expect it.

Zwieback answered 24/1, 2019 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.