从限制setter方法​​来防止内部视觉工作室 [英] Prevent visual studio from limiting the setter method to internal

查看:161
本文介绍了从限制setter方法​​来防止内部视觉工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我使用Visual Studio 2015 CE,更新2.
一劈生产率我通常做的是,我创建空模型类,如:

 公共类PersonModel 
{
}

然后用它们在选择表达式,如:

  db.People.Where(p => someCondition)
。选择(p =>新建PersonModel
{
n = p.Id,
名称= p.Name,
//设置其他属性
})。了ToList();



然后我去了又非现有属性编号名称,...并按控制+ 问Visual Studio中生成属性Id 我。
所有伟大的,但它会创建:

 公众诠释标识{搞定;内部设置; } 



如果我用同样的方法在一个asp.net的WebAPI模型绑定,绑定操作将失败默默并给我 ID = 0



所以我的问题是:有没有任何选项来问VS创建公共setter方法​​,例如:

 公众诠释标识{搞定;组; } 


解决方案

设置的访问修饰符内部,因为当前的代码库是一个合适的。在你的情况,你正在引用到已创建的属性是一样的组件内使内部访问修饰符是最合适的。我不认为你可以修改此行为。



如果您使用ReSharper的然后它会为你创建公共二传手。另一个解决办法是将你想要这个行为到另一个组件的类。


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; }

解决方案

It set access modifier to internal because base on current code it is the appropriate one. In your case, you are referencing to the created property just inside the same assembly so internal access modifier is the most suitable one. I don't think you can modify this behavior.

If you use Resharper then it will create the public setter for you. Another workaround is to move the classes you want this behavior for them to another assembly.

这篇关于从限制setter方法​​来防止内部视觉工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆