防止visual studio将setter方法限制为内部

时间:2023-01-14 18:48:03

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

好吧,我使用visual studio 2015 CE,更新2.我通常做的一个生产力黑客是我创建空模型类,如:

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:

然后我转到尚未存在的属性Id和Name,...并按Control +。要求v​​isual studio为我生成属性ID。一切都很好,但它会创造:

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.

如果我在asp.net webapi模型绑定中使用相同的方法,绑定将无提示失败并给我Id = 0。

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

所以我的问题是:有没有选择要求VS创建公共制定者,即:

public int Id { get; set; }

1 个解决方案

#1


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.

您可以通过键入prop并按两次TAB来完成此操作。它将要求您输入属性的类型和名称,但不会从现有属性中提取它。

Best regards.

#1


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.

您可以通过键入prop并按两次TAB来完成此操作。它将要求您输入属性的类型和名称,但不会从现有属性中提取它。

Best regards.