防止 Visual Studio 将 setter 方法限制为内部 [英] Prevent visual studio from limiting the setter method to internal
问题描述
好吧,我使用的是 Visual Studio 2015 CE,更新 2.我通常做的一种生产力技巧是创建空模型类,例如:
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();
然后我转到尚不存在的属性 Id
和 Name
,...然后按 Control+.
要求 Visual Studio为我生成属性 ID
.一切都很棒,但它会创造:
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; }
如果我在 asp.net webapi 模型绑定中使用相同的方法,绑定将无声地失败并给我 Id = 0
.
and if I use the same method in an asp.net webapi model binding, the binding will fail silently and give me Id = 0
.
所以我的问题是:是否有任何选项可以让 VS 创建公共设置器,即:
So my question is: is there any option to ask VS to create public setter, i.e.:
public int Id { get; set; }
推荐答案
所以我的问题是:是否有任何选项可以让 VS 创建公共设置器,即:
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.
这篇关于防止 Visual Studio 将 setter 方法限制为内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!