CLS 规范的理由:属性及其访问器的可访问性必须相同
刚刚运行了 Parasoft 代码分析工具的规则集。
public int testProperty // violation
{
private get // not matching property accessibility
{ return _testValue; }
set
{ _testValue = value; }
}
使它们都匹配的修复。原因指向 此 MSDN 页面上的属性部分CLS。然而,页面上并未提及这样做的理由。
我经常使用表单的自动属性 public int MyProp { 获取; private set;}
这是否违反了 CLS? RFC
Was just running through the ruleset of Parasoft's code analysis tool.
public int testProperty // violation
{
private get // not matching property accessibility
{ return _testValue; }
set
{ _testValue = value; }
}
The fix to make them both match. The reason points to the properties section on this MSDN Page on the CLS. However the justification for this is not mentioned on the page.
I frequently use automatic properties of the formpublic int MyProp { get; private set;}
Is this a violation of the CLS ? RFC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看该页面的更新版本 (甚至是 .NET 2.0版本)它没有这个规则。基本上它在 v1.1 和 v2.0 之间消失了...同时 C# 开始允许以不同的方式指定它们:)
这是一个愚蠢的规则,也是 C# 1 中愚蠢的功能缺失,IMO。拥有私有 setter 和公共 getter 显然很有用。诚然,相反的情况很少见……
If you look at a more recent version of that page (or even the .NET 2.0 version) it doesn't have that rule. Basically it went away between v1.1 and v2.0... at the same time when C# started allowing them to be specified differently :)
It was a silly rule, and a silly lack-of-feature in C# 1, IMO. It's obviously useful to be able to have a private setter and a public getter. It's pretty rare to have it the other way round, admittedly...